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

Can't resolve 'crypto', BREAKING CHANGE: webpack <5 used to include polyfills? #136

Open
dclechok opened this issue Jul 26, 2022 · 3 comments

Comments

@dclechok
Copy link

dclechok commented Jul 26, 2022

Hi, I recently installed this package to use in a project, and I'm getting this warning/error:

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\dclec\Documents\Mawa\beyp\front-end\node_modules\bcryptjs\dist'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill 
for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
        - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:     
        resolve.fallback: { "crypto": false }

I'm currently using Node v14.16.0. I don't know if I need to "include a polyfill", or where (which file(s)) to even add these details.... I'm relatively newer to development, could someone maybe help me understand what is going on and what I can do to fix this?

@dcodeIO
Copy link
Owner

dcodeIO commented Jul 26, 2022

The code in question is here

bcrypt.js/src/bcrypt.js

Lines 22 to 34 in 7e2e93a

function random(len) {
/* node */ if (typeof module !== 'undefined' && module && module['exports'])
try {
return require("crypto")['randomBytes'](len);
} catch (e) {}
/* WCA */ try {
var a; (self['crypto']||self['msCrypto'])['getRandomValues'](a = new Uint32Array(len));
return Array.prototype.slice.call(a);
} catch (e) {}
/* fallback */ if (!randomFallback)
throw Error("Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative");
return randomFallback(len);
}

where if the module is supposed to be run under Node.js, the crypto package is used to obtain proper random numbers. If running under Node.js is not required, I guess the crypto module can be stubbed out with webpack's second suggestion. Not sure if Node.js meanwhile got their stuff together and made crypto available globally to match browsers. If not I hope that happens sometime.

@dclechok
Copy link
Author

I went with the second option, since trying the first caused a ton more errors. Everything seems to be functional, and the error went away... Being newer to this, I'm not sure there is anything to worry about here.

@calebTree
Copy link

calebTree commented Aug 18, 2022

Inside: node_modules\bcryptjs\package.json, update the browser key to:

"browser": {
  "bin": "dist/bcrypt.js",
  "crypto": false
},

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

3 participants