Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

axios

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;
like image 548
dev Avatar asked Jul 14 '18 08:07

dev


People also ask

How do you resolve Axios in react?

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 and restart your development server. Copied!

How do I fix module not found in react?

To solve the "Module not found: Can't resolve" error in React, make sure to install the package from the error message if it's a third-party package, e.g. npm i somePackage . If you got the error when importing local files, correct your import path.

Can't resolve URL in Axios react?

Check your App. js to make sure your import has the correct path (whether you really should have a '../axios' or just 'axios') Stop the old development server (Ctrl + C) Restart the development server (yarn start)


2 Answers

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';

like image 163
Aleksandar G Avatar answered Oct 03 '22 20:10

Aleksandar G


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

like image 27
Shashank Prasad Avatar answered Oct 03 '22 21:10

Shashank Prasad