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;
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!
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.
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)
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';
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With