Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Module not found: Can't resolve 'crypto' (or fs, http, https, path, stream) after moving to webpack 5

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

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

Fix it fast

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

    1. 1Can'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. 2Can'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. 3The 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. 4The build passes, but the browser console reports that process is not defined.process is used in browser codeFix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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 | head

2Fix

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 serve

Confirmed by webpack.js.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

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