Module not found: Can't resolve 'crypto' (or fs, http, https, path, stream) after moving to webpack 5
Module not found: Error: Can't resolve 'crypto'Short answer
Webpack 5 no longer adds browser versions of Node.js core modules, so any browser code that imports crypto, http, https, stream or fs fails to resolve. Either install a browser package and map it in resolve.fallback, or set the module to false when the code path never runs in the browser.
Fix it fast
1Find out which cause you have
Find which package pulls the Node.js module in. The build error prints the import chain; the first package after your own files is the one that needs a fallback.
npm ls crypto-browserify stream-browserify buffer path-browserify2Match what you see, then jump to its fix
- 1
Can't resolve 'crypto' (or http, https, stream, path) in a file under node_modules.A dependency needs a Node.js module and the browser has noneFix - 2
Can't resolve 'fs' or 'net' or 'tls' from a library that only uses it on the server side.The module is imported but never used in the browserFix - 3
The config still has a node block from webpack 4, and the build reports unresolved modules.An old node: { fs: 'empty' } option is left in the configFix - 4
The build passes, but the browser console reports that process is not defined.process is used in browser codeFix
- 1
What does the error look like?
Module not found: Error: Can't resolve 'crypto'Can't resolve 'fs'Can't resolve 'https'
Cause 1 of 4
A dependency needs a Node.js module and the browser has none
1Check
Webpack 5 no longer polyfills Node.js core modules automatically. If browser code uses one, you must install a compatible package from npm and configure resolve.fallback yourself.
npm ls --all 2>/dev/null | grep -i -E "web3|crypto|stream"2Fix
Install a browser replacement and map the module to it. Add one entry per module named in the error.
npm install --save-dev crypto-browserify stream-browserify buffer path-browserify
// webpack.config.js
module.exports = {
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer'),
path: require.resolve('path-browserify'),
},
},
};3Verify
Rebuild. Each module you mapped stops appearing in the error list.
npx webpackConfirmed by webpack.js.org
Cause 2 of 4
The module is imported but never used in the browser
1Check
Modules such as fs have no meaningful browser version. If the code path is server-only, shipping a polyfill is pointless.
npx webpack 2>&1 | grep "Can't resolve"2Fix
Set those entries to false. Webpack then leaves them out of the bundle.
module.exports = {
resolve: {
fallback: {
fs: false,
net: false,
tls: false,
},
},
};3Verify
Rebuild and load the page; if the app calls the missing module at run time, map it to a real package instead.
npx webpackConfirmed by webpack.js.org
Cause 3 of 4
An old node: { fs: 'empty' } option is left in the config
1Check
Webpack 5 removed the node.* options for these modules. The migration guide says to replace node.fs: 'empty' with resolve.fallback.fs: false.
grep -n -A6 "node:" webpack.config.*2Fix
Move each old node.<module> setting into resolve.fallback.
// before
node: { fs: 'empty' },
// after
resolve: { fallback: { fs: false } },3Verify
Rebuild; the Can't resolve error for that module is gone.
npx webpackConfirmed by webpack.js.org
Cause 4 of 4
process is used in browser code
1Check
Webpack 5 no longer includes a polyfill for the process variable. The guide says to avoid it in frontend code.
grep -rn "process\." src | head2Fix
Replace the uses with DefinePlugin or EnvironmentPlugin values, or guard them with typeof process !== 'undefined'.
new webpack.DefinePlugin({
'process.env.API_URL': JSON.stringify(process.env.API_URL),
})3Verify
Open the page and check the console for the ReferenceError.
npx webpack serveConfirmed by webpack.js.org
How do I stop it coming back?
- Before moving a project from webpack 4 to 5, read the migration guide's section on removed polyfills and list the Node.js modules your dependencies use.
- Prefer libraries that ship a browser build instead of polyfilling a server one.
- Add fallback entries only for modules named in an error; a long list of polyfills grows the bundle.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix