Webpack 5 "Module not found: ... node:crypto" - Fix Polyfill fallback
Webpack 5 stopped auto-polyfilling Node core modules (crypto, stream, buffer, path) for the browser. A dependency that imports them now fails to resolve unless you provide a fallback polyfill or tell Webpack the module is unused in the browser.
What this error means
A Webpack 5 build fails with Module not found: Error: Can't resolve 'crypto' plus the note "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default." It appears after upgrading to Webpack 5.
Module not found: Error: Can't resolve 'crypto' in '/app/node_modules/some-lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core
modules by default. This is no longer the case. ... install 'crypto-browserify'
and add resolve.fallback or set resolve.fallback to false.Common causes
Dependency imports a Node core module
A browser-targeted dependency requires crypto/stream/buffer. Webpack 5 no longer ships a browser polyfill automatically, so resolution fails.
Server-only code reaching the browser bundle
Sometimes the import is a leak - a server-only module pulled into the client bundle - in which case a polyfill is the wrong fix and the dependency should be excluded instead.
How to fix it
Provide a browser polyfill fallback
Install the browser shim and map the module in resolve.fallback.
npm install -D crypto-browserify stream-browserify
// webpack.config.js
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
},
},Or disable the fallback when truly unused
If the browser never needs the module, set its fallback to false so Webpack stubs it out.
// webpack.config.js
resolve: { fallback: { crypto: false, fs: false } },How to prevent it
- Add
resolve.fallbackentries for Node core modules browser deps require. - Keep server-only modules out of the client bundle so polyfills are not needed.
- Audit
resolve.fallbackwhen upgrading to or across Webpack 5.