Maaz Zindani

Loading the index…

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

Vite says a Node.js module was externalized for the browser

ErrorModule "fs" has been externalized for browser compatibility. Cannot access "fs.readFile" in client code.

Short answer

Vite does not polyfill Node.js built-ins. When code that runs in the browser imports one, such as fs or path, Vite logs this warning and the call you make on it cannot work in client code. Find the importing file, then remove that import from client code or add a polyfill yourself.

  • Applies to Vite 7, Node.js 22, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The message names the module. Search your own source for it first; if nothing matches, a dependency is the importer.

    grep -rn "from 'fs'\|from \"fs\"\|require('fs')" src/
    npm ls --all 2>/dev/null | head -40
  2. 2Match what you see, then jump to its fix

    1. 1grep finds the import in a file under src/ that the browser loadsYour own browser code imports a Node.js moduleFix
    2. 2grep finds nothing in src/, and npm ls shows a package that pulls in a Node.js-only libraryA dependency meant for the browser imports the moduleFix

What does the error look like?

  • Module "fs" has been externalized for browser compatibility. Cannot access "fs.readFile" in client code.

Cause 1 of 2

Your own browser code imports a Node.js module

1Check

Open the file the grep found and see whether it is bundled for the client. File reading, path joining and similar work belongs on a server, not in the page.

grep -rn "from 'node:\|from 'path'\|from 'fs'" src/

2Fix

Move the Node.js work to a server or build script and let the browser call it through an HTTP request. If the browser really needs the behaviour, avoid the Node.js module and, only where needed, add a polyfill by hand.

// before (client code)
import { readFile } from 'fs';

// after: ask your server for the data instead
const data = await fetch('/api/file').then((r) => r.json());

3Verify

Build again and confirm the warning is gone.

npm run build

Confirmed by vite.dev

Did this fix it?

Cause 2 of 2

A dependency meant for the browser imports the module

1Check

Find which package depends on the library that uses the Node.js module. Vite names the module but the importer is inside node_modules.

npm explain <package-name>

2Fix

Vite's guide says to report this to the library, since a package built for the browser should not import Node.js modules. Meanwhile, use a browser-ready alternative package.

# example: replace the Node-only package with a browser-ready one
npm uninstall <node-only-package>
npm install <browser-ready-package>

3Verify

Rebuild and check that the warning no longer lists that module.

npm run build

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Keep server-only code (files, databases, secrets) in a separate folder that no client file imports.
  • Check a package's documentation for a browser build before adding it to a front-end project.
  • Treat this warning as a bug to fix, not noise: the message says the call cannot be accessed in client code.

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.