bcrypt.js icon indicating copy to clipboard operation
bcrypt.js copied to clipboard

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

Open AshleyJBartlett opened this issue 5 years ago • 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.

AshleyJBartlett avatar Jun 11 '19 08:06 AshleyJBartlett

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 avatar Jun 11 '19 10:06 dcodeIO

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

AshleyJBartlett avatar Jun 11 '19 11:06 AshleyJBartlett

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

Peelz avatar Jul 12 '19 04:07 Peelz

I get the same error in angular-cli 8.1.0.

ChrisMakrutzki avatar Jul 22 '19 12:07 ChrisMakrutzki

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.

dcodeIO avatar Jul 22 '19 14:07 dcodeIO

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.

akammaru avatar Aug 05 '19 08:08 akammaru

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

nickdotht avatar Aug 21 '19 07:08 nickdotht

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

papakay avatar Oct 08 '19 02:10 papakay

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

zhyale avatar Feb 15 '20 09:02 zhyale

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

kdlogan19 avatar Jul 17 '20 15:07 kdlogan19

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

sertal70 avatar Dec 28 '20 13:12 sertal70

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

rdsedmundo avatar Apr 26 '21 13:04 rdsedmundo

I have equal problem but i have not idea

Benandry avatar Mar 30 '23 10:03 Benandry

How to solve this issue/warning?

rajann44 avatar Apr 02 '23 22:04 rajann44

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

Benandry avatar Apr 03 '23 06:04 Benandry

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

I thought this is the same repo?

quannq-vvt avatar Jun 15 '23 11:06 quannq-vvt