Skip to content

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

Closed

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

Activity

  1. added
    freq1: low
    on May 12, 2025
  2. added 2 commits that reference this issue on May 12, 2025
    c5e120d
    bb2137b
  3. added 2 commits that reference this issue on May 12, 2025
    5093ce0
    0098c38
  4. added a commit that references this issue on May 12, 2025
    0ae9c1f
  5. angular-automatic-lock-bot commented on Jun 12, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    freq1: lowseverity5: regressiontype: bug/fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions