Maaz Zindani

Loading the index…

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

webpack 5 cannot resolve an import that has no file extension

ErrorModule not found

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

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

Fix it fast

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

    1. 1The 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. 2The 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. 3The failing import ends in a folder name, for example './components'.An import points at a directoryFix

What does the error look like?

  • Module not found
  • Can'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 | head

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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'" | head

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

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