Maaz Zindani

Loading the index…

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

webpack cannot resolve an import (Module not found)

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

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

Fix it fast

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

    1. 1Can't resolve './something' in '/path/to/folder'The relative path is wrongFix
    2. 2The 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. 3Can't resolve 'react-dom' (a bare name, with no ./ at the start)A package is not installed, such as react-domFix
    4. 4Can't resolve '@components/Button', or Can't resolve 'path' or 'fs' in browser codeAn alias or a Node.js core module is missingFix

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 header

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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-dom

3Verify

Rebuild.

npx webpack

Confirmed by react.dev

Did this fix it?

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

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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