webpack: external type 'module' used in a script that has no dynamic import()
webpack build fails for an external of type module in a script bundleShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
externalsType is 'module' and output.module is not trueexternalsType module without ES module outputFix - 2
You use await import('lib') and the bundle must stay a normal scriptThe external is only used through dynamic import()Fix - 3
Both import x from 'lib' and import('lib') exist for externalsSome externals are loaded statically and some dynamicallyFix - 4
output.environment.dynamicImport is false (the default) for your targetThe build target says dynamic import is not availableFix
- 1
What does the error look like?
webpack build fails for an external of type module in a script bundlewebpack 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.js2Fix
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 webpackConfirmed by webpack.js.org
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/ | head2Fix
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 -3Confirmed by webpack.js.org
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 --version2Fix
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 webpackConfirmed by webpack.js.org
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.js2Fix
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 webpackConfirmed by webpack.js.org
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 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