Skip to content

ESM builder with top-level await fails to load on node ≥ 20.19.0 #30286

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

Closed
1 task
bgotink opened this issue May 12, 2025 · 0 comments · Fixed by #30288
Closed
1 task

ESM builder with top-level await fails to load on node ≥ 20.19.0 #30286

bgotink opened this issue May 12, 2025 · 0 comments · Fixed by #30288
Labels
freq1: low Only reported by a handful of users who observe it rarely severity5: regression type: bug/fix

Comments

@bgotink
Copy link
Contributor

bgotink commented May 12, 2025

Command

build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Node 20 has added support for require() to load ESM files, though only ESM files that don't use top-level await.

This breaks the angular CLI loading those modules, since it now fails with with ERR_REQUIRE_ASYNC_MODULE rather than ERR_REQUIRE_ESM

// The file could be either CommonJS or ESM.
// CommonJS is tried first then ESM if loading fails.
try {
return localRequire(builderPath);
} catch (e) {
if ((e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ESM') {
// Load the ESM configuration file using the TypeScript dynamic import workaround.
// Once TypeScript provides support for keeping the dynamic import this workaround can be
// changed to a direct dynamic import.
return (await loadEsmModule<{ default: unknown }>(pathToFileURL(builderPath))).default;
}
throw e;
}

Minimal Reproduction

Have a builder that uses top-level await, try to use the builder

Exception or Error

Error: unexpected top-level await at file:///home/jenkins/workspace/<redacted>
     esbuild = await import("esbuild");
               ^

An unhandled exception occurred: 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.
See "/tmp/ng-me8w9s/angular-errors.log" for further details.

Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 19.1.7
Node: 20.19.0
Package Manager: yarn 4.0.1
OS: darwin arm64

Angular: undefined
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1901.7
@angular-devkit/core         19.1.7
@angular-devkit/schematics   19.1.7
@angular/cli                 19.1.7
@schematics/angular          19.1.7
typescript                   5.2.2

Anything else relevant?

No response

@alan-agius4 alan-agius4 added type: bug/fix freq1: low Only reported by a handful of users who observe it rarely severity5: regression labels May 12, 2025
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue May 12, 2025
…M modules

Resolve `ERR_REQUIRE_ASYNC_MODULE` when attempting to `require()` ESM modules in a CommonJS context.

Closes angular#30286
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue May 12, 2025
…M modules

Resolve `ERR_REQUIRE_ASYNC_MODULE` when attempting to `require()` ESM modules in a CommonJS context.

Closes angular#30286
@clydin clydin closed this as completed in 0ae9c1f May 12, 2025
clydin pushed a commit that referenced this issue May 12, 2025
…M modules

Resolve `ERR_REQUIRE_ASYNC_MODULE` when attempting to `require()` ESM modules in a CommonJS context.

Closes #30286

(cherry picked from commit 0ae9c1f)
clydin pushed a commit that referenced this issue May 12, 2025
…M modules

Resolve `ERR_REQUIRE_ASYNC_MODULE` when attempting to `require()` ESM modules in a CommonJS context.

Closes #30286

(cherry picked from commit 0ae9c1f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
freq1: low Only reported by a handful of users who observe it rarely severity5: regression type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants