Skip to content

webpack-based dev-server crashing when loading ESM based proxy.conf.mjs #33652

Description

@vz-tl

Command

serve

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

20

Description

We're on upgrading our mono-repo hosting several Angular apps from NG20 to NG21, where some apps are using esbuild-based application-builder and some are still stuck with webpack.

For the webpack based apps, we encounter an error crashing the local (ssr) dev-server on startup:

Must use import to load ES Module: /home/.../proxy.conf.mjs
require() of ES modules is not supported.
require() of /home/.../proxy.conf.mjs from /home/lampert/projects/hilti/hdms-frontend/node_modules/@angular/build/src/utils/load-proxy-config.js is an ES module file as it is a .mjs file whose nearest parent package.json contains "type": "module" which defines all .mjs files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/.../package.json.

As to be seen in the above message, the proxy-config we're using is in ESM format.

application-builder/ esbuild based dev-server seems not being affected.

I tried to debug the issue and tracked it down to the following location:
node_modules/@angular/build/src/utils/load-proxy-config.js:
https://github.com/angular/angular-cli/blob/21.0.x/packages/angular/build/src/utils/load-proxy-config.ts#L57

      try {
        proxyConfiguration = await import(proxyPath);
      } catch (e) {
        assertIsError(e);
        if (e.code !== 'ERR_REQUIRE_ASYNC_MODULE') {
          throw e;
        }

        proxyConfiguration = await loadEsmModule<{ default: unknown }>(pathToFileURL(proxyPath));
      }

The above logic will throw on any error with a different code than ERR_REQUIRE_ASYNC_MODULE, but in my case the error code is ERR_REQUIRE_ESM.

Looking at the v20 version of
https://github.com/angular/angular-cli/blob/20.3.x/packages/angular/build/src/utils/load-proxy-config.ts#L69
the additional error code is still present

        if (e.code === 'ERR_REQUIRE_ESM' || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
          proxyConfiguration = await loadEsmModule<{ default: unknown }>(pathToFileURL(proxyPath));
          break;
        }

After locally patching node_modules/@angular/build/src/utils/load-proxy-config.js dev-server started working again with a fully functional proxy config.

So I'm requesting to add the error code back to make webpack-based dev servers with esm proxy configs working again. Thx in advance!

Minimal Reproduction

Exception or Error


Your Environment

Angular CLI       : 21.2.19
Angular           : 21.2.18
Node.js           : 24.15.0
Package Manager   : npm 11.12.1
Operating System  : linux x64

┌───────────────────────────────┬───────────────────┬───────────────────┐
│ Package                       │ Installed Version │ Requested Version │
├───────────────────────────────┼───────────────────┼───────────────────┤
│ @angular-devkit/build-angular │ 21.2.19           │ 21.2.19           │
│ @angular-devkit/core          │ 21.2.19           │ 21.2.19           │
│ @angular-devkit/schematics    │ 21.2.19           │ 21.2.19           │
│ @angular/cdk                  │ 21.2.14           │ 21.2.14           │
│ @angular/cli                  │ 21.2.19           │ 21.2.19           │
│ @angular/common               │ 21.2.18           │ 21.2.18           │
│ @angular/compiler             │ 21.2.18           │ 21.2.18           │
│ @angular/compiler-cli         │ 21.2.18           │ 21.2.18           │
│ @angular/core                 │ 21.2.18           │ 21.2.18           │
│ @angular/forms                │ 21.2.18           │ 21.2.18           │
│ @angular/google-maps          │ 21.2.14           │ 21.2.14           │
│ @angular/language-service     │ 21.2.18           │ 21.2.18           │
│ @angular/platform-browser     │ 21.2.18           │ 21.2.18           │
│ @angular/platform-server      │ 21.2.18           │ 21.2.18           │
│ @angular/router               │ 21.2.18           │ 21.2.18           │
│ @angular/ssr                  │ 21.2.18           │ 21.2.18           │
│ @angular/youtube-player       │ 21.2.14           │ 21.2.14           │
│ @schematics/angular           │ 21.2.19           │ 21.2.19           │
│ ng-packagr                    │ 21.2.5            │ 21.2.5            │
│ rxjs                          │ 7.8.2             │ 7.8.2             │
│ typescript                    │ 5.9.3             │ 5.9.3             │
│ zone.js                       │ 0.15.1            │ 0.15.1            │
└───────────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Activity

  1. added this to the needsTriage milestone on Jul 24, 2026
  2. changed the title [-]webpack-based dev-server crashing hen loading ESM based proxy.conf.mjs[/-] [+]webpack-based dev-server crashing when loading ESM based proxy.conf.mjs[/+] on Jul 24, 2026
  3. alan-agius4 commented on Jul 27, 2026

    @alan-agius4
    Collaborator

    It looks like you're running an unsupported Node.js version. Even though your ng version is correct, maybe ng serve is being invoked from a different context? Modern Node versions allow loading .mjs modules directly.

    The ERR_REQUIRE_ESM error is deprecated in Node.js v20.19.0, v22.12.0, or v23.0.0 and newer.

    Double-check which Node runtime is currently active in your environment.

  4. vz-tl commented on Jul 27, 2026

    @vz-tl
    Author

    Thx @alan-agius4 , as I already added above, my local Node version is v24.15.0 - so looks like pretty much well supported. Worth noting my package.json does not have any type defined (neither commonjs nor module).

    Maybe the deprecation of ERR_REQUIRE_ESM doesn't imply, as long not fully removed there won't be scenarios where this error code still is being returned? Hence the cleanup might be a bit early?

  5. alan-agius4 commented on Jul 27, 2026

    @alan-agius4
    Collaborator

    I tested this locally and wasn't able to replicate the issue. In addition, we already have unit tests covering this scenario here: https://github.com/angular/angular-cli/blob/main/packages/angular/build/src/builders/dev-server/tests/options/proxy-config_spec.ts

  6. vz-tl commented on Jul 27, 2026

    @vz-tl
    Author

    I don't question this has been properly tested accordingly, also what you mentioned actually sounds quite logical. However, it's not just me getting this error, but all my team mates on the same setup are affected. So what else could be a reason for Node returning a deprecated error code other than it still could happen to be returned, regardless of the deprecation?

  7. alan-agius4 commented on Jul 27, 2026

    @alan-agius4
    Collaborator

    Looking at the Node.js source code https://github.com/nodejs/node/blob/9e6f8a04847c825bd0f0e4ea5990f98332a8c1a3/lib/internal/modules/esm/loader.js#L468, this error appears to only trigger when requiring ES modules is explicitly disabled.

  8. vz-tl commented on Jul 27, 2026

    @vz-tl
    Author

    I wouldn't know we explicitly disabled requiring ES modules or how to do that other than by adding "type": "module" to package.json , which we didn't 🤔

    Additionally, as there's still 2 locations of this error-code in the Node sources, wouldn't this mean there still IS a chance it can be thrown?

  9. alan-agius4 commented on Jul 27, 2026

    @alan-agius4
    Collaborator

    These parts of the error message you posted above Must use import to load ES Module: and require() of ES modules is not supported. have been bothering me quite a bit, as this wording was the error message that was displayed in older versions of Node.js.

    That error message has been changed years ago and I managed to find the PR nodejs/node#39175

    If you, change the proxy to a CJS file, and console log, process.version does it show the expected version? I suspect that an old version is being used.

  10. alan-agius4 commented on Jul 27, 2026

    @alan-agius4
    Collaborator

    Additionally, as there's still 2 locations of this error-code in the Node sources, wouldn't this mean there still IS a chance it can be thrown?

    Only if require of ESM modules is disabled.

  11. vz-tl commented on Jul 27, 2026

    @vz-tl
    Author

    If you, change the proxy to a CJS file, and console log, process.version does it show the expected version? I suspect that an old version is being used.

    Output of console.log(process.version) in renamed proxy.conf.js: v24.15.0

  12. angular-automatic-lock-bot commented on Aug 27, 2026

    @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.

  13. locked and limited conversation to collaborators on Aug 27, 2026
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions