Skip to content

Angular application fails to polyfill WebCrypto for libraries that depend on it #28673

@TernaryFortress

Description

@TernaryFortress

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

I'm attempting to use the https://github.com/paulmillr/noble-curves library in my angular project, however whenever I pass my new service into a constructor, I get hit with this error:

✨ Compiled Worker successfully
✨ Uploading Worker bundle
✨ Uploading _routes.json
🌎 Deploying...

✘ [ERROR] Deployment failed!

  Failed to publish your Function. Got error: Uncaught Error: No such module
  "crypto".
    imported from "chunk-TUV5YX6S.mjs"

It's worth mentioning that I'm using cloudflare's quick-start:

npm create cloudflare@latest -- my-angular-app --framework=angular

which has some wrangler built-ins


The problem here, is that noble relies on the WebCrypto getRandomNumbers method, which isn't being exposed. We also should ideally not be using a polyfill, due to the added security vulnerabilities, however those are the only solutions I'm finding online.


HOWEVER, even without that assumption, attempting to add either "crypto-browserify" or "crypto-js" to paths in ts.config also fails, and trying to expose crypto as a dependency via "allowedCommonJsDependencies": ["crypto"]" in angular.json fails to get results too.

Minimal Reproduction

https://github.com/TernaryFortress/angular_debug

You can just download my version and npm run serve or deploy it, if you want.

Exception or Error

Failed to publish your Function. Got error: Uncaught Error: No such module
"crypto".
imported from "chunk-TUV5YX6S.mjs"

Your Environment

Angular CLI: 18.2.7
Node: 20.18.0
Package Manager: npm 10.8.2
OS: linux x64

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

Package Version

@angular-devkit/architect 0.1802.7 (cli-only)
@angular-devkit/build-angular
@angular-devkit/core 18.2.7 (cli-only)
@angular-devkit/schematics 18.2.7 (cli-only)
@angular/cli 18.2.7 (cli-only)
@schematics/angular 18.2.7 (cli-only)
rxjs 7.8.1 (cli-only)
typescript
zone.js

Anything else relevant?

We should be trying to explicitly expose the browser's crypto module if possible, as using a polyfill is going to result in vulnerabilities that don't otherwise need to be there.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions