Maaz Zindani

Loading the index…

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

webpack: external type 'module' used in a script that has no dynamic import()

Errorwebpack build fails for an external of type module in a script bundle

Short answer

Externals of type 'module' generate static import statements, which a classic script cannot contain. For a script build you either turn on output.module, or switch the external to the 'import' type, which generates a dynamic import(). Since webpack 5.94 'module-import' picks per usage.

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

Fix it fast

  1. 1Find out which cause you have

    Print the three settings that decide this: externalsType, output.module and output.environment.dynamicImport.

    grep -n "externalsType\|module:\|dynamicImport\|externals" webpack.config.js
  2. 2Match what you see, then jump to its fix

    1. 1externalsType is 'module' and output.module is not trueexternalsType module without ES module outputFix
    2. 2You use await import('lib') and the bundle must stay a normal scriptThe external is only used through dynamic import()Fix
    3. 3Both import x from 'lib' and import('lib') exist for externalsSome externals are loaded statically and some dynamicallyFix
    4. 4output.environment.dynamicImport is false (the default) for your targetThe build target says dynamic import is not availableFix

What does the error look like?

  • webpack build fails for an external of type module in a script bundle
  • webpack reports that the target environment does not support dynamic import()

Cause 1 of 4

externalsType module without ES module output

1Check

The docs say webpack generates import * as X from '...' for such externals and that output.module must be enabled first, otherwise webpack throws.

grep -n "externalsType\|output" webpack.config.js

2Fix

Turn on output.module, and load the bundle with a script of type module.

export default {
  output: { module: true },
  externalsType: 'module',
  externals: { jquery: 'jquery' },
};

3Verify

The build runs; load the file with <script type="module">.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 4

The external is only used through dynamic import()

1Check

The 'import' type is described as generating code like import('...') for externals, and it is noted that the output bundle will then contain an import() statement.

grep -rn "import(" src/ | head

2Fix

Set the externals type to 'import'. This type does not require output.module.

export default {
  externalsType: 'import',
  externals: { jquery: 'jquery' },
};

3Verify

Rebuild; the bundle holds an import() call for the external.

npx webpack
grep -n "import(" dist/*.js | head -3

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 4

Some externals are loaded statically and some dynamically

1Check

From webpack 5.94 the 'module-import' type chooses 'module' for static imports and 'import' for dynamic ones. Static imports still need output.module.

npx webpack --version

2Fix

Use 'module-import' with output.module enabled.

export default {
  output: { module: true },
  externalsType: 'module-import',
  externals: { jquery: 'jquery' },
};

3Verify

Rebuild and check both kinds of import in the output.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 4 of 4

The build target says dynamic import is not available

1Check

output.environment.dynamicImport tells webpack whether the runtime supports an async import() function for ES modules; the docs list it as false in their example.

grep -n "environment\|target" webpack.config.js

2Fix

If your runtime really supports import(), tell webpack so.

output: {
  environment: { dynamicImport: true },
},

3Verify

Rebuild; the error about dynamic import support no longer appears.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Decide once whether the output is a classic script or an ES module and set output.module to match.
  • Use 'import' or 'module-import' for externals that are loaded lazily.
  • Set output.environment.dynamicImport only for runtimes you know support it.

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.