Webpack 5 "BREAKING CHANGE: webpack < 5 used to include polyfills" in CI
Webpack 5 stopped automatically polyfilling Node core modules such as crypto, stream, and buffer for browser bundles. Any code that imports them now fails to resolve unless you supply a fallback yourself.
What this error means
The build fails with "Module not found: Error: Can't resolve 'crypto'" followed by a "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default" note.
Module not found: Error: Can't resolve 'stream' in '/app/src'
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.Common causes
Browser code imports a Node core module
A dependency or your code imports crypto/stream/buffer for a web target, which Webpack 5 no longer polyfills automatically.
A migration from Webpack 4 without fallbacks
Code that relied on Webpack 4 auto-polyfills breaks on 5 because resolve.fallback is now required for those modules.
How to fix it
Add explicit fallbacks for needed modules
- Decide whether the browser bundle truly needs the Node module.
- Install the polyfill package and map it in
resolve.fallback. - Set the fallback to
falsefor modules you do not need.
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
fs: false,
},
},Remove the Node-only dependency
If the import is server-only, exclude that code path from the browser bundle so no polyfill is needed.
How to prevent it
- Add
resolve.fallbackentries when migrating to Webpack 5. - Keep server-only code out of browser bundles.
- Audit which Node core modules your web build really requires.