Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack build fails on fs, path or node:crypto in browser code

ErrorModule 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.

  • Applies to webpack 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1Can't resolve 'fs' with a trace ending in node_modulesA library imports a Node.js module the browser never usesFix
    2. 2Can'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. 3The 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

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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.js

2Fix

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.js

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.