Vite says a Node.js module was externalized for the browser
Module "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.
Fix it fast
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 -402Match what you see, then jump to its fix
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 buildConfirmed by vite.dev
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 buildConfirmed by vite.dev
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 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