Skip to content

Compatibility issue after migration to v19 (node-forge) #29099

@kuzjka

Description

@kuzjka

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

18.2.11

Description

Hello,

We use node-forge in our Angular app. It is distributed as CommonJS module, and we are aware of the downsides. However, it used to work well in browser, as it has heuristics to learn the environment where the code is executed.

We have switched to new builder as soon as it was released with Angular 17. So it seems the breaking change is introduced in v19.

After migration to Angular v19, it looks like ng serve tries to execute/analyze the code during build stage and falls into a branch, which is intended for NodeJS environment.

Probably, it is related to server-side rendering optimizations introduced in version 19, which makes development server execute the code.

I would be grateful if there is any workaround to set development server behavior similar to what existed in v18.

Thank you.

Minimal Reproduction

  1. Create new project with Angular v19

  2. Install typings for node-forge: npm i @types/node-forge

  3. Import and use forge in any component. Example:

    // other imports
    import * as forge from 'node-forge';
    
    @Component({/* ... */})
    export class AppComponent {
      title = 'tmp-ng';
      something = forge.util.isArray([]);
    }
  4. ng serve

Exception or Error

X [ERROR] The package "crypto" wasn't found on the file system but is built into node. [plugin angular-browser-node-built-in]

    node_modules/node-forge/lib/prng.js:16:20:
      16 │   _crypto = require('crypto');
         ╵                     ~~~~~~~~


### and after first request to the Vite server:


Waiting for the debugger to disconnect...
C:\Data\projects\kms-web-ui\node_modules\vite\node_modules\esbuild\lib\main.js:1472
  let error = new Error(text);
              ^

Error: Build failed with 1 error:
node_modules/node-forge/lib/prng.js:16:20: ERROR: [plugin: angular-browser-node-built-in] The package "crypto" wasn't found on the file system but is built into node.
    at failureErrorWithLog (C:\Data\projects\kms-web-ui\node_modules\vite\node_modules\esbuild\lib\main.js:1472:15)
    at C:\Data\projects\kms-web-ui\node_modules\vite\node_modules\esbuild\lib\main.js:945:25
    at C:\Data\projects\kms-web-ui\node_modules\vite\node_modules\esbuild\lib\main.js:1353:9
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errors: [Getter/Setter],
  warnings: [Getter/Setter]
}

Node.js v20.11.1

Your Environment

Angular CLI: 19.0.4
Node: 20.11.1
Package Manager: npm 10.8.1
OS: win32 x64

Angular: 19.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.4
@angular-devkit/build-angular   19.0.4
@angular-devkit/core            19.0.4 (cli-only)
@angular-devkit/schematics      19.0.4
@angular/cdk                    19.0.2
@angular/cli                    19.0.4
@angular/material               19.0.2
@schematics/angular             19.0.4
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.15.0

Anything else relevant?

The require line is executed under condition forge.util.isNodejs:

https://github.com/digitalbazaar/forge/blob/2bb97afb5058285ef09bcf1d04d6bd6b87cffd58/lib/prng.js#L16-L19

where

https://github.com/digitalbazaar/forge/blob/2bb97afb5058285ef09bcf1d04d6bd6b87cffd58/lib/util.js#L109-L111

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