webpack cannot resolve an import (Module not found)
Module not found: Error: Can't resolve './components/Header' in '/app/src'Short answer
Can't resolve means webpack searched every place its resolver knows and found no file. The error line shows the request and the folder it started from. Compare the two. Usual causes are a wrong relative path, a file extension that is not in resolve.extensions, a missing alias, or a package that is not installed.
Fix it fast
1Find out which cause you have
Read the error line. The text in the first quotes is the request, and the path in the second quotes is the folder webpack started from. Check that folder on disk.
ls -la src/components2Match what you see, then jump to its fix
- 1
Can't resolve './something' in '/path/to/folder'The relative path is wrongFix - 2
The file exists, but the import has no extension and the file ends in .jsx, .ts or .tsxThe file extension is not in resolve.extensionsFix - 3
Can't resolve 'react-dom' (a bare name, with no ./ at the start)A package is not installed, such as react-domFix - 4
Can't resolve '@components/Button', or Can't resolve 'path' or 'fs' in browser codeAn alias or a Node.js core module is missingFix
- 1
What does the error look like?
Module not found: Error: Can't resolve './components/Header' in '/app/src'Module not found: Error: Can't resolve 'react-dom' in '/app/src'Cannot resolve module 'react-dom'
Cause 1 of 4
The relative path is wrong
1Check
A relative request is joined to the folder of the file that holds the import. Join the two by hand and see whether a file exists there. Case counts on Linux: Header.js and header.js are different files.
ls src/components | grep -i header2Fix
Correct the path in the import so it points at the real file name, with the right upper and lower case.
// src/App.js
import Header from './components/Header';3Verify
Rebuild and watch for the error to go away.
npx webpackConfirmed by webpack.js.org
Cause 2 of 4
The file extension is not in resolve.extensions
1Check
When an import has no extension, webpack tries the list in resolve.extensions. The default list is .js, .json and .wasm only.
grep -n -A3 "extensions" webpack.config.js2Fix
Add the extensions you use. The three dots keep the defaults in the list.
// webpack.config.js
module.exports = {
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '...'],
},
};3Verify
Rebuild.
npx webpackConfirmed by webpack.js.org
Cause 3 of 4
A package is not installed, such as react-dom
1Check
A bare name is searched in the folders of resolve.modules, which is node_modules by default. Check that the package is there.
ls node_modules | grep -E "^react(-dom)?$"2Fix
Install it. React needs both packages.
npm install react react-dom3Verify
Rebuild.
npx webpackConfirmed by react.dev
Cause 4 of 4
An alias or a Node.js core module is missing
1Check
A name like @components only works if resolve.alias defines it. Webpack 5 also stopped polyfilling Node.js core modules for the browser.
grep -n -A6 "alias\|fallback" webpack.config.js2Fix
Define the alias with an absolute path. For a core module, set a fallback to false, or to a browser package you installed.
const path = require('path');
module.exports = {
resolve: {
alias: { '@components': path.resolve(__dirname, 'src/components/') },
fallback: { fs: false },
},
};3Verify
Rebuild.
npx webpackConfirmed by webpack.js.org
How do I stop it coming back?
- Use one case style for file names and let CI run on Linux, where a case mistake fails.
- Define aliases once in webpack and mirror them in tsconfig paths.
- Commit the lock file and run npm ci in CI so packages match.
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
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
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
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