dropbox-sdk-js icon indicating copy to clipboard operation
dropbox-sdk-js copied to clipboard

Can't resolve 'crypto' - updating webpack to v.5

Open danidev opened this issue 4 years ago • 7 comments

Hello,

To people like me that updated a project to webpack 5 and dropbox 9 and got this error:

ERROR in ./node_modules/dropbox/dist/Dropbox-sdk.min.js 1:30619-30636
Module not found: Error: Can't resolve 'crypto' in '/.../node_modules/dropbox/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 }

The solution I've found was:

install node-polyfill-webpack-plugin

and on webpack config:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
	// Other rules...
	plugins: [
		new NodePolyfillPlugin()
	]
}

Hope this will help, I've lost a lot of time to figuring out it...

danidev avatar Apr 10 '21 10:04 danidev

Thanks for writing this up and sharing it!

greg-db avatar Apr 12 '21 18:04 greg-db

Thank you! So helpful. My site was a Laravel Vue site so it was slightly different. I installed the node-polyfill-webpack-plugin like you suggested.

npm install node-polyfill-webpack-plugin

And, I had to add the following code to webpack.mix.js

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
mix.webpackConfig(webpack => {
    return {
        plugins: [
            new NodePolyfillPlugin()
        ]
    };
});

lorismithgenies avatar May 18 '21 13:05 lorismithgenies

For anyone using Create React App (can't edit webpack config), this will fix it

justinmoon avatar Dec 30 '21 01:12 justinmoon

thanks @danidev! your solution worked for me after much frustration 💚

theawkwardchild avatar May 06 '22 10:05 theawkwardchild

I also just ran into this.

Should this open up a wider discussion regarding the use of this package? If it's designed to be used both in the browser and in a Node environment, why is it relying on Node core modules? If it's not designed to be used in the browser, does it make sense to create a second package that is?

mheavenor avatar Aug 15 '22 18:08 mheavenor

Had the same errors after updating Vue from 3.0.5 to currently the last 3.2.37 which uses Webpack 5. node-polyfill-webpack-plugin helps, thank you a lot!

mlukianovich avatar Aug 25 '22 10:08 mlukianovich

Thank you sooooo much! I spent whole afternoon to fix it:) Two tips: 1.If you build your project using creat-react-app, the webpack.config.js is in node_modules/react-scripts/config 2.Be careful,new NodePolyfillPlugin()should be set in plugins not resolve.plugins. I made this mistake at first time.

ydeera avatar Oct 20 '22 12:10 ydeera