Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails if webpack.config.js uses top-level await #4340

Closed
s100 opened this issue Dec 6, 2024 · 0 comments · Fixed by #4346
Closed

Fails if webpack.config.js uses top-level await #4340

s100 opened this issue Dec 6, 2024 · 0 comments · Fixed by #4346
Labels

Comments

@s100
Copy link

s100 commented Dec 6, 2024

Describe the bug

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 require some 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'
}

To Reproduce

package.json:

{
  "type": "module",
  "devDependencies": {
    "webpack-cli": "^5.1.4"
  }
}

webpack.config.js:

await undefined
export default {
  // ... any correct webpack config
}

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 information

  System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
    Memory: 12.98 GB / 31.71 GB
  Binaries:
    Node: 22.12.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (128.0.2739.54)
    Internet Explorer: 11.0.22621.3527
  Packages:
    webpack-cli: ^5.1.4 => 5.1.4

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:

try {
result = require(module);
} catch (error) {
const dynamicImportLoader: null | DynamicImport<T> =
require("./utils/dynamic-import-loader")();
if (
((error as ImportLoaderError).code === "ERR_REQUIRE_ESM" ||

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant