webpack 5 cannot resolve an import that has no file extension
Module not foundShort answer
In .mjs files, and in .js files under a package.json with type module, webpack 5 resolves imports as fully specified. It adds no extension and does not open directories. Add the extension to your import, point at the index file, or set resolve.fullySpecified to false in a rule that matches only the files that need it.
Fix it fast
1Find out which cause you have
Open the file named in the error and check two things: whether it ends in .mjs, and whether the nearest package.json has a type field set to module.
grep -n '"type"' package.json node_modules/some-package/package.json2Match what you see, then jump to its fix
- 1
The path in the error is inside node_modules and the file is .mjs or sits under a package with type module.A dependency uses extensionless imports in strict ESM modeFix - 2
The failing import is in your src folder, for example import { x } from './utils'.Your own code is in .mjs or a type module package and omits extensionsFix - 3
The failing import ends in a folder name, for example './components'.An import points at a directoryFix
- 1
What does the error look like?
Module not foundCan't resolve
Cause 1 of 3
A dependency uses extensionless imports in strict ESM mode
1Check
The docs say that for .mjs files and for .js files under a package.json with type module, webpack requires the extension and fails with Module not found without it. You cannot edit the dependency, so relax the rule for it.
grep -n '"type"' node_modules/some-package/package.json
ls node_modules/some-package/dist | head2Fix
Turn fullySpecified off with a module rule, and limit it with include to the one package so your own code keeps the strict behaviour.
// webpack.config.js
const path = require('path');
module.exports = {
module: {
rules: [
{
test: /\.m?js$/,
include: path.resolve(__dirname, 'node_modules/some-package'),
resolve: { fullySpecified: false },
},
],
},
};3Verify
Run the build again; the Module not found line for that package is gone.
npx webpack --mode developmentConfirmed by webpack.js.org
Cause 2 of 3
Your own code is in .mjs or a type module package and omits extensions
1Check
The same rule applies to your files: under type module, webpack will not try .js for you.
grep -rn "from '\./" src | grep -v "\.js'" | head2Fix
Write the full file name in the import. This also keeps the code valid for Node.js, which has the same requirement.
// before
import { x } from './utils';
// after
import { x } from './utils.js';3Verify
The build passes without any change to the webpack config.
npx webpack --mode developmentConfirmed by webpack.js.org
Cause 3 of 3
An import points at a directory
1Check
With fully specified resolution, webpack does not resolve a directory to the file named in resolve.mainFiles; you must give the file name yourself.
ls src/components2Fix
Import the index file explicitly.
// before
import { Button } from './components';
// after
import { Button } from './components/index.js';3Verify
The error for that import is gone on the next build.
npx webpack --mode developmentConfirmed by webpack.js.org
How do I stop it coming back?
- Write extensions in every relative import once a package is type module.
- Scope fullySpecified: false with include instead of applying it to all of node_modules.
- Check a new dependency's package.json type field when an upgrade introduces this error.
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
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 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
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 fix