Skip to content

Angular production build with ESBuild does not honor "browser" field in package.json, leading to unresolved Node.js core modules #30558

Closed as duplicate of#27033
@andsouto

Description

@andsouto

Which @angular/* package(s) are the source of the bug?

compiler-cli, Don't known / other

Is this a regression?

Yes

Description

When building an Angular application for production, the build fails with errors related to unresolved Node.js core modules (e.g., node:http, node:fs, etc.). This occurs because the dependency @xmpp/resolve uses the "browser" field in its package.json to indicate that certain modules should be replaced or ignored for browser builds. However, the Angular build system (using ESBuild for production builds) does not honor the "browser" field, unlike the previous Webpack-based setup.

Expected Behavior

The build process should respect the "browser" field in dependencies' package.json files, replacing or ignoring Node.js-only modules as indicated, resulting in a successful production build.

Actual Behavior

The build fails with errors similar to the following:

✘ [ERROR] Could not resolve "node:http"
✘ [ERROR] Could not resolve "node:https"
✘ [ERROR] Could not resolve "node:fs"
...

This did not occur with older versions of Angular that used Webpack for production builds.

Steps to Reproduce

  1. Clone the example repository:
    git clone https://github.com/andsouto/angular-build-error-example.git
  2. Install dependencies:
    cd angular-build-error-example && npm install
  3. Attempt to build the project for production:
    ng build
  4. Observe the build failure with unresolved Node.js module errors.

Please provide a link to a minimal reproduction of the bug

https://github.com/andsouto/angular-build-error-example

Please provide the exception or error you saw

Application bundle generation failed. [1.285 seconds]

✘ [ERROR] Could not resolve "node:http"

    (disabled):node_modules/node-fetch/src/index.js:9:17:
      9 │ import http from 'node:http';
        ╵                  ~~~~~~~~~~~

  The package "node:http" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:https"

    (disabled):node_modules/node-fetch/src/index.js:10:18:
      10 │ import https from 'node:https';
         ╵                   ~~~~~~~~~~~~

  The package "node:https" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:zlib"

    (disabled):node_modules/node-fetch/src/index.js:11:17:
      11 │ import zlib from 'node:zlib';
         ╵                  ~~~~~~~~~~~

  The package "node:zlib" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:stream"

    (disabled):node_modules/node-fetch/src/index.js:12:54:
      12 │ import Stream, { PassThrough, pipeline as pump } from 'node:stream';
         ╵                                                       ~~~~~~~~~~~~~

  The package "node:stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:buffer"

    (disabled):node_modules/node-fetch/src/index.js:13:23:
      13 │ import { Buffer } from 'node:buffer';
         ╵                        ~~~~~~~~~~~~~

  The package "node:buffer" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:fs"

    node_modules/fetch-blob/from.js:1:59:
      1 │ import { statSync, createReadStream, promises as fs } from 'node:fs';
        ╵                                                            ~~~~~~~~~

  The package "node:fs" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:path"

    node_modules/fetch-blob/from.js:2:25:
      2 │ import { basename } from 'node:path';
        ╵                          ~~~~~~~~~~~

  The package "node:path" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:stream"

    node_modules/node-fetch/src/body.js:7:36:
      7 │ import Stream, { PassThrough } from 'node:stream';
        ╵                                     ~~~~~~~~~~~~~

  The package "node:stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:util"

    node_modules/node-fetch/src/body.js:8:44:
      8 │ import { types, deprecate, promisify } from 'node:util';
        ╵                                             ~~~~~~~~~~~

  The package "node:util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:buffer"

    node_modules/node-fetch/src/body.js:9:23:
      9 │ import { Buffer } from 'node:buffer';
        ╵                        ~~~~~~~~~~~~~

  The package "node:buffer" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:util"

    node_modules/node-fetch/src/headers.js:7:22:
      7 │ import { types } from 'node:util';
        ╵                       ~~~~~~~~~~~

  The package "node:util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:http"

    node_modules/node-fetch/src/headers.js:8:17:
      8 │ import http from 'node:http';
        ╵                  ~~~~~~~~~~~

  The package "node:http" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:url"

    node_modules/node-fetch/src/request.js:9:36:
      9 │ import { format as formatUrl } from 'node:url';
        ╵                                     ~~~~~~~~~~

  The package "node:url" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:util"

    node_modules/node-fetch/src/request.js:10:26:
      10 │ import { deprecate } from 'node:util';
         ╵                           ~~~~~~~~~~~

  The package "node:util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


✘ [ERROR] Could not resolve "node:net"

    node_modules/node-fetch/src/utils/referrer.js:1:21:
      1 │ import { isIP } from 'node:net';
        ╵                      ~~~~~~~~~~

  The package "node:net" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.0.3
Node: 22.13.0
Package Manager: npm 10.9.2
OS: linux x64

Angular: 20.0.3
... build, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2000.3
@angular-devkit/core         20.0.3
@angular-devkit/schematics   20.0.3
@schematics/angular          20.0.3
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions