Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.0.1 leading to Crypto error and asking to setRandomFallback #160

Open
ndbhamvani opened this issue Feb 18, 2025 · 2 comments
Open

v3.0.1 leading to Crypto error and asking to setRandomFallback #160

ndbhamvani opened this issue Feb 18, 2025 · 2 comments

Comments

@ndbhamvani
Copy link

ndbhamvani commented Feb 18, 2025

Summary
For a GraphQL application, I'm using bcryptjs and it was working without any errors whilst v2.4.3 was being used.
However, on upgrade to v3.0.0 or v.3.0.1, I get the followiong error:-
Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative

Forcing v2.4.3 resolved this error, but that's a very old version now.

Node Version: 18.16.1

@dcodeIO
Copy link
Owner

dcodeIO commented Feb 18, 2025

In 3.0.0, the method of obtaining the crypto module in Node.js has changed, so that's likely what's causing the issue.

For a bit of background: Since 3.0.0, when bcrypt.js is imported as an ECMAScript module, there's now a normal import crypto from "crypto", whereas when it is imported as a CommonJS module, there's a require("crypto"). In Node.js 23+, Web Crypto API has become available globally, so that'll be used, but that condition doesn't apply here.

There are a bunch of tests to assert that these mechanism are working when the library is consumed, but perhaps your application utilizes another method of consuming the library. Is the build trying to bundle it perhaps? Asking because if that's the case, it might be that the bundler is consuming the ECMAScript module now, potentially omitting the crypto import. Just a guess ofc. Could you provide more details on how the library is used?

@dcodeIO
Copy link
Owner

dcodeIO commented Feb 18, 2025

Might have spotted the issue, let me know if 3.0.2 fixes the problem for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants