19

I'm getting this compilation error in my React project where I try to send a GET request:

./src/Component/Form.js
Module not found: Can't resolve '../axios' in 'F:\React\react-complete-guide\src\Component'

CODE:

import React, {Component} from 'react';

import axios from '../axios';

class Form extends React.Component{

state={UserName:""};

onChangeHandle=(event)=>{
    this.setState({UserName:event.target.value});

}

handleSubmit= (event) =>{

event.preventDefault();
console.log('form submit');

axios.get('https://api.github.com/users/${this.state.UserName}')
   .then(
       resp=>{
       console.log(resp);
    })

};


render(){
    return(
        <form onSubmit={this.handleSubmit}>
            <input type="text" 
                placeholder="Github UserName"
                value={this.state.UserName}
                onChange={this.onChangeHandle}   />
            <br/>
            <button type="submit"> Add card </button>
        </form>    
    )}
}

export default Form;
7
  • It seems axios is not imported/installed properly. Is this axios, npm package or some file in your project? How are you importing it? Please share the code of src/Component/Form.js to see how you have imported it. Commented Jul 14, 2018 at 9:28
  • I have added the code. Commented Jul 14, 2018 at 20:51
  • 1
    It seems that axios node module is imported incorrectly. Instead of import axios from '../axios'; it should be like import axios from 'axios'; Commented Jul 14, 2018 at 22:30
  • I have also tried the above solution ....but not work Commented Jul 16, 2018 at 7:07
  • 3
    Can you add the github issue link here? I installed the axios package (npm install axios --save) and was able to successfully import the package by import axios from 'axios' Commented Jul 18, 2018 at 15:35

10 Answers 10

45

Try:

1. Installing axios module with npm: npm install axios --save

2. Replacing your import code: import axios from '../axios'; with the: import axios from 'axios';

Sign up to request clarification or add additional context in comments.

3 Comments

npm install axios --save,saved me,thanks for the answer,can you explain whats mean by --save flag?
You're welcome :) --save basically tells npm to add the package to the dependencies list.
--save is not needed for npm 5.0.0 and above stackoverflow.com/questions/19578796/…
7

The code

import axios from '../axios';

Is for importing a file, and the '../ ' is the path to the upper folder. Hence "../axios" means it's looking for a file "axios.js" in the outer folder of the current file.

An axios file is created to create an instance of axios to have some default parameters set as baseURL, intercepters etc.

Here, you have to import the module axios,given that you already have installed axios with,

npm install axios --save

You can import it as,

import axios from 'axios';

Replace your import axios line with the above line

Comments

3

In your second line, please look at the error

import axios from '../axios';

that should be

import axios from 'axios';

Comments

1

For me the issue was that I hadn't installed the axios module in my project folder. Make sure you are in the project directory when installing the axios module using:npm install axios --save

After installation simply run it using

import axios from 'axios';

Comments

1

Its working fine for me.

  • Installing axios module with npm: npm install axios --save

Comments

1

I once had that error but I fixed it with npm install axios --save.

Comments

0

The error message means that the axios package is missng and needs to be installed. Run below command in your project root directory, to install it.

npm install axios --save

1 Comment

Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful.
0

just open project directory after npm start, you might be seeing error like this: error message

after that, just type: npm i axios

in cmd opened in project directory and hit enter. after this the axios will be installed in project. as: view of console After this, if you re start by npm start, you may get message that port 3000 is already in use, so use other port, type Y, hit enter, and you should be fine to go.

Comments

0

In case anybody uses yarn, do

yarn add anxios

Comments

0

To solve the error "Module not found: Error: Can't resolve 'axios'", make sure to install the axios package by opening your terminal in your project's root directory and running the command:

npm install axios --save

or

yarn add axios

then restart your development server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.