You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Node.js 22.12.0+, if webpack.config.js is an ES module which makes use of top-level await, webpack-cli fails.
What is the current behavior?
Starting from Node.js 22.12.0, Node.js is now able to successfully requiresome ES modules, provided that the ES module being loaded does not use top-level await and nor do any of the modules it loads in turn. (This functionality has existed for some time behind a flag, but it is now enabled by default.) If the ES module being loaded does use top-level await, the require call throws a new ERR_REQUIRE_ASYNC_MODULE exception. webpack-cli does not catch this and so fails:
[webpack-cli] Failed to load 'C:\...\webpack.config.js' config
[webpack-cli] Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
at ModuleJobSync.runSync (node:internal/modules/esm/module_job:392:13)
at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
at loadESMFromCJS (node:internal/modules/cjs/loader:1414:24)
at Module._compile (node:internal/modules/cjs/loader:1547:5)
at Object..js (node:internal/modules/cjs/loader:1677:16)
at Module.load (node:internal/modules/cjs/loader:1318:32)
at Function._load (node:internal/modules/cjs/loader:1128:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
at Module.require (node:internal/modules/cjs/loader:1340:12) {
code: 'ERR_REQUIRE_ASYNC_MODULE'
}
webpack-cli does already include code which is intended to handle the case where require(...) of the webpack.config.js fails, and fall back to import(...) instead:
Up until now this was behaving correctly because ERR_REQUIRE_ESM was what was thrown in this case. As of Node.js 22.12.0, however, the error thrown is a previously unseen ERR_REQUIRE_ASYNC_MODULE, which does not get caught and handled in the same way and so continues to bubble up.
Additional context
In our case the top-level await was an fsPromises.readFile() call - we were able to eliminate this and use fs.readFileSync() instead as a workaround.
The text was updated successfully, but these errors were encountered:
Describe the bug
In Node.js 22.12.0+, if
webpack.config.js
is an ES module which makes use of top-levelawait
,webpack-cli
fails.What is the current behavior?
Starting from Node.js 22.12.0, Node.js is now able to successfully
require
some ES modules, provided that the ES module being loaded does not use top-levelawait
and nor do any of the modules it loads in turn. (This functionality has existed for some time behind a flag, but it is now enabled by default.) If the ES module being loaded does use top-levelawait
, therequire
call throws a newERR_REQUIRE_ASYNC_MODULE
exception.webpack-cli
does not catch this and so fails:To Reproduce
package.json
:webpack.config.js
:Expected behavior
This webpack build should run to completion normally with any version of Node.js.
Screenshots
Please paste the results of
npx webpack-cli info
here, and mention other relevant informationwebpack-cli
does already include code which is intended to handle the case whererequire(...)
of thewebpack.config.js
fails, and fall back toimport(...)
instead:webpack-cli/packages/webpack-cli/src/webpack-cli.ts
Lines 347 to 353 in 29e6e6b
Up until now this was behaving correctly because
ERR_REQUIRE_ESM
was what was thrown in this case. As of Node.js 22.12.0, however, the error thrown is a previously unseenERR_REQUIRE_ASYNC_MODULE
, which does not get caught and handled in the same way and so continues to bubble up.Additional context
In our case the top-level
await
was anfsPromises.readFile()
call - we were able to eliminate this and usefs.readFileSync()
instead as a workaround.The text was updated successfully, but these errors were encountered: