Skip to content

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

Closed
@bgotink

Description

@bgotink

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions