Fix guides / Next.js: "Module not found: Can't resolve 'fs'"

How to fix Next.js "Module not found: Can't resolve 'fs'"

The build or dev server stops with Module not found: Can't resolve 'fs', or the same for net, tls, path or child_process. Something that only runs in Node is being bundled for the browser.

Why it happens

Browsers have no file system. If a file marked 'use client', or any file it imports, imports a Node library (a database driver, nodemailer, fs, a server SDK), Next.js tries to ship it to the browser and fails. The import chain is often indirect: a client component imports a utils.ts that also imports your database.

Fix 1: find the chain

The error shows an "Import trace". Read it from the bottom: the first file with 'use client' is where the server code leaks in.

Fix 2: move the server work to the server

// app/actions.ts
'use server';
import { db } from '@/lib/db';
export async function getOrders() {
  return db.order.findMany();
}

// components/Orders.tsx
'use client';
import { getOrders } from '@/app/actions';

An API route (app/api/.../route.ts) works too. Split mixed helper files into lib/server/* and lib/shared/*.

Fix 3: lock it so it never happens again

npm install server-only
// first line of lib/db.ts
import 'server-only';

Now any client import of that file fails with a clear message.

What not to do

Adding resolve.fallback = { fs: false } to the webpack config silences the error but the code will still crash in the browser. Only use it for a library that merely checks for fs and never calls it.

Check the fix

  1. npm run build passes.
  2. The feature works in a production preview.
Still stuck? No shame in it: these bugs hide in places your AI builder cannot see. Send me the error and your repo and I will fix it as a pull request within 24 hours, explained in plain English, for $49. Several things broken? The $149 Full Rescue covers up to 3 issues plus a security pass, within 72 hours. Not fixed means full refund.
Quick Fix, $49Full Rescue, $149Ask first

Last updated 2026-10-21.