dcodeIO/bcrypt.js

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

AshleyJBartlett opened this issue ยท 16 comments

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.

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.

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

Peelz commented

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

I get the same error in angular-cli 8.1.0.

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.

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.

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

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

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' }

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

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

@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.

I have equal problem but i have not idea

How to solve this issue/warning?

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

I thought this is the same repo?