webpack build fails on fs, path or node:crypto in browser code
Module not found: Error: Can't resolve 'fs'Short answer
webpack 5 no longer polyfills Node.js core modules. If browser code, or a library it uses, imports fs, path or node:crypto, the build fails. Set resolve.fallback to false for modules the browser never needs, install a polyfill for ones it does, or build with target node if the code runs on the server.
Fix it fast
1Find out which cause you have
Read the import trace under the error. It names the file that asks for the module. Decide whether that file runs in the browser or on Node.js.
npx webpack 2>&1 | grep -B2 -A8 "Can't resolve\|UnhandledScheme"2Match what you see, then jump to its fix
- 1
Can't resolve 'fs' with a trace ending in node_modulesA library imports a Node.js module the browser never usesFix - 2
Can't resolve 'path' or 'crypto', or UnhandledSchemeError for a node: importYour browser code really needs the module, and webpack 5 does not polyfill itFix - 3
The failing file is server code (a build script, an SSR entry or a CLI)The code runs on Node.js but is bundled for the web targetFix
- 1
What does the error look like?
Module not found: Error: Can't resolve 'fs'Webpack ERROR in node:crypto Module build failed: UnhandledSchemeError: Reading from "node:crypto"
Cause 1 of 3
A library imports a Node.js module the browser never uses
1Check
Many packages require fs only on a server code path. In a browser bundle that import has nothing to resolve to.
npm ls --all 2>/dev/null | grep -i "<package-from-trace>"2Fix
Tell webpack to leave the module out. Setting a fallback entry to false excludes it.
// webpack.config.js
module.exports = {
resolve: {
fallback: {
fs: false,
},
},
};3Verify
Rebuild and check that the error is gone.
npx webpackConfirmed by webpack.js.org
Cause 2 of 3
Your browser code really needs the module, and webpack 5 does not polyfill it
1Check
webpack 5 no longer includes polyfills for Node.js core modules. Code that runs in the browser must get a compatible npm package from you.
grep -rn "require('path')\|from 'node:" src/2Fix
Install a browser-compatible package and map it in resolve.fallback. For a node: import, rewrite the request so the prefix is dropped and the fallback can match.
npm install --save-dev path-browserify crypto-browserify stream-browserify
// webpack.config.js
const webpack = require('webpack');
module.exports = {
resolve: {
fallback: {
path: require.resolve('path-browserify'),
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
},
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => {
resource.request = resource.request.replace(/^node:/, '');
}),
],
};3Verify
Rebuild; the import should now resolve to the browser package.
npx webpackConfirmed by webpack.js.org
Cause 3 of 3
The code runs on Node.js but is bundled for the web target
1Check
A bundle for Node.js must not be built for the browser. Look at the target in your config.
grep -n "target" webpack.config.js2Fix
Build that bundle with target node and the node externals preset, so built-in modules are loaded with require() at run time instead of bundled.
// webpack.config.js
module.exports = {
target: 'node',
externalsPresets: { node: true },
};3Verify
Rebuild and run the output with Node.js.
npx webpack
node dist/main.jsConfirmed by webpack.js.org
How do I stop it coming back?
- Prefer browser-safe packages for code that ships to the client.
- Keep server and client entries in separate webpack configs with their own target.
- Add polyfills one module at a time, only after the import trace shows they are needed.
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
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix