Can't resolve '../axios' in a React project
Module not found: Can't resolve '../axios' in 'F:\React\react-complete-guide\src\Component'Short answer
A path that starts with ./ or ../ is a file path, not the axios package. The bundler looks for a file or folder named axios next to the importing file and finds none. Import the package by its bare name, or fix the relative path to your own axios.js and its extension.
Fix it fast
1Find out which cause you have
Read the import line, then list the folder it points at. Compare the path to what is on disk.
grep -rn "from '../axios'\|from \"../axios\"" src/ ls src2Match what you see, then jump to its fix
- 1
import axios from '../axios' and no file named axios in the parent folderA relative path was used for the packageFix - 2
You have a file such as axios.js or axios-instance.js and the import does not point at itYou meant your own axios instance file, and the path is wrongFix - 3
The file exists, e.g. axios.jsx or axios.ts, and the import still failsThe file has an extension the bundler does not tryFix
- 1
What does the error look like?
Module not found: Can't resolve '../axios' in 'F:\React\react-complete-guide\src\Component'Module not found: Error: Can't resolve '../axios'
Cause 1 of 3
A relative path was used for the package
1Check
Paths starting with ./ or ../ are resolved from the importing file's folder as files or folders. A bare name such as axios is searched in node_modules instead.
ls node_modules/axios/package.json2Fix
Import the package by its bare name, and install it if the check shows it missing.
npm install axios
// in the component
import axios from "axios";3Verify
The build no longer reports the module.
npm run buildConfirmed by nodejs.org
Cause 2 of 3
You meant your own axios instance file, and the path is wrong
1Check
Relative paths are tried as the exact file name, then with .js, .json and .node by Node's algorithm; webpack's default list is .js, .json, .wasm. List the folder and compare names, including the exact folder depth.
ls src/api2Fix
Make the relative path match the file location. An instance file is created with axios.create and exported, then imported with a path that matches where it is.
// src/api/axios.js
import axios from "axios";
export default axios.create({ baseURL: "https://api.example.com" });
// src/Component/Posts.js
import instance from "../api/axios";3Verify
The import resolves and the build passes.
npm run buildConfirmed by axios.rest
Cause 3 of 3
The file has an extension the bundler does not try
1Check
Webpack tries the extensions in resolve.extensions, by default .js, .json and .wasm. Setting your own list replaces the defaults. A project that sets it without '...' stops finding files that used to work.
grep -rn "extensions" webpack.config.js craco.config.js 2>/dev/null2Fix
Add the missing extension to the list, keeping the defaults with '...', or write the full file name in the import.
resolve: {
extensions: [".jsx", "..."],
},3Verify
The import resolves without naming the extension.
npm run buildConfirmed by webpack.js.org
How do I stop it coming back?
- Use bare names for packages and relative paths only for your own files.
- Keep one api folder for the shared axios instance and import it from there everywhere.
- Let your editor update imports when you move a file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
webpack cannot resolve an import (Module not found)
Module not found: Error: Can't resolve 'react-dom' in '/app/src'
Can't resolve means webpack searched every place its resolver knows and found no file.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix