bcrypt.js
bcrypt.js copied to clipboard
Can't resolve 'crypto', BREAKING CHANGE: webpack <5 used to include polyfills?
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?
The code in question is here
https://github.com/dcodeIO/bcrypt.js/blob/7e2e93af99df2952253f9cf32db29aefa8f272f7/src/bcrypt.js#L22-L34
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.
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.
Inside: node_modules\bcryptjs\package.json, update the browser key to:
"browser": {
"bin": "dist/bcrypt.js",
"crypto": false
},