dcodeIO / bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
Other
3.47k stars 264 forks source link

Error: Can't resolve 'crypto' in '\node_modules\bcryptjs\dist' #101

Open AshleyJBartlett opened 5 years ago

AshleyJBartlett commented 5 years ago

Hi,

I am using bcryptjs (latest version of 2.4.3) in my app developed using Ionic framework.

I am getting the following come up: [ng] WARNING in ./node_modules/bcryptjs/dist/bcrypt.js [ng] Module not found: Error: Can't resolve 'crypto' in '\node_modules\bcryptjs\dist'

Any help will be much appreciated.

dcodeIO commented 5 years ago

Seems that the library assumes a node environment, thus loading the crypto module to obtain secure random numbers, but actually isn't in a node environment.

AshleyJBartlett commented 5 years ago

@dcodeIO - Thank you for your prompt response. What do you suggest I do to resolve this?

Peelz commented 4 years ago

I've get same problem in react-native, because they not included "crypto" package.

ChrisMakrutzki commented 4 years ago

I get the same error in angular-cli 8.1.0.

dcodeIO commented 4 years ago

So, part of the problem here seems to be that the module attempts to require the crypto module first, expecting that it can catch the resulting error if this fails, but some environments do not allow catching the error but instead abort execution. That about correct?

Maybe one way to deal with this is to invert the checks, first attempting WCA and only then attempting to require the crypto module. Whether this works or not depends on the availability of WCA of course, because if it isn't present, the same error will occur again when attempting the alternative. Someone here willing to try this by patching the library locally?

Additionally, a better check could be added to only attempt requiring the crypto module when sufficiently sure that the code is running inside of a node environment, since without that, the fallback logic will never be able to run.

akammaru commented 4 years ago

I have tried to use bcryptjs in React native and get the same error. After playing about with the code for a bit, I haven't been able to get it to work. What was sugested, to use WCA didn't work. Its either I don't understand the code or does WCA also use crypto? Since I have not been able to find a bcrypt package for React native expo so far, I hope either someone can point me in the right direction or I can find a way to get this one to work.

edit so it would seem it is... as usual, an issue with expo not exposing the nodeJs core but having it's own package system and names.

nickdotht commented 4 years ago

I've been using react-native-bcrypt instead. Works great.

papakay commented 4 years ago

I'm having this issue in ember js. Any help please?

zhyale commented 4 years ago

Edit:
node_modules\@angular-devkit\build-angular\src\angular-cli-files\models\webpack-configs\browser.js

set node to new value:

node: { crypto: true, stream: true, fs: 'empty', net: 'empty' }

kdlogan19 commented 3 years ago

Any update on this? I am getting the same error. I am working with react-native.

sertal70 commented 3 years ago

This happened to me when debugging my angular app with ng serve, I solved it by adding the following to my package.json:

...
  "browser": {
    "crypto": false
  },
...

Credits go to this SO answer

rdsedmundo commented 3 years ago

@dcodeIO

the problem here seems to be that the module attempts to require the crypto module first, expecting that it can catch the resulting error if this fails, but some environments do not allow catching the error but instead abort execution. That about correct?

Yes, that is exactly what happens.

Benandry commented 1 year ago

I have equal problem but i have not idea

rajann44 commented 1 year ago

How to solve this issue/warning?

Benandry commented 1 year ago

I add this line in file package.json "browser": { "crypto": false }

On Mon, Apr 3, 2023 at 1:21 AM Rajan Chaudhary @.***> wrote:

How to solve this issue/warning?

— Reply to this email directly, view it on GitHub https://github.com/dcodeIO/bcrypt.js/issues/101#issuecomment-1493453865, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUSZD6YGDC2T3E5PTH37H5LW7H3WJANCNFSM4HW36QNA . You are receiving this because you commented.Message ID: <dcodeIO/bcrypt. @.***>

quannq-vvt commented 1 year ago

I've been using react-native-bcrypt instead. Works great.

I thought this is the same repo?