Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Can't resolve '../axios' in a React project

ErrorModule 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.

  • Applies to webpack 5, Node.js 22, axios 1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 src
  2. 2Match what you see, then jump to its fix

    1. 1import axios from '../axios' and no file named axios in the parent folderA relative path was used for the packageFix
    2. 2You 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. 3The file exists, e.g. axios.jsx or axios.ts, and the import still failsThe file has an extension the bundler does not tryFix

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.json

2Fix

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 build

Confirmed by nodejs.org

Did this fix it?

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/api

2Fix

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 build

Confirmed by axios.rest

Did this fix it?

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/null

2Fix

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 build

Confirmed by webpack.js.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.