Module not found: Can't resolve 'fs'
Module not found: Can't resolve 'fs'
Import trace for requested module:
./lib/db.ts
./app/components/List.tsxIn one sentence: Code meant for the server (that uses Node's fs, path or a database library) got imported into a component that runs in the browser.
What it means
`fs` is Node's file system module. Browsers don't have it, so the bundler fails when browser code imports it, directly or through a library like a database driver.
The import trace under the error shows the chain: the last file listed is usually a client component that imports server-only code.
Common causes
- A `"use client"` component imports a helper that reads files or talks to a database.
- A server-only library (Prisma, pg, mysql2, nodemailer) is used in client code.
- Pages Router code uses `fs` outside `getServerSideProps` or `getStaticProps`.
How to fix it
- Follow the import trace and find the client component that pulls in the server code.
- Move the server work into a Server Component, a server action, or an API route, and pass only the data to the client component.
- Add `import "server-only";` to the top of server helpers so this mistake fails with a clear message next time.
- Don't add `fs: false` webpack fallbacks to hide the error; the code still won't work in the browser.
Before
"use client";
import { getPosts } from "@/lib/db"; // uses fs / database
export default function List() { /* ... */ }After
// app/page.tsx (Server Component)
import { getPosts } from "@/lib/db";
import List from "./List";
export default async function Page() {
return <List posts={await getPosts()} />;
}Tried this and it's still broken? Paste your exact error for a free explanation of your specific case.
Prompt to paste into your AI tool
Next.js says Module not found: Can't resolve 'fs'. Follow the import trace, find the client component importing server-only code, move that data loading into a Server Component, server action or API route, pass the data down as props, and add import 'server-only' to the server helper. Don't change unrelated code.Still stuck?
Paste your exact error and code. Get a plain-English explanation free, or the full fix for $5.
Hitting errors every day? Builder is $15/mo for 100 fixes per month. Compare plans
Related errors
- Next.js: params should be awaited before using its properties
- Error: supabaseUrl is required
- Error: NEXT_REDIRECT
- TypeScript: Type 'string | undefined' is not assignable to type 'string'
Weekly: the errors breaking vibe-coded apps and how to fix them. Free.
No spam. Unsubscribe anytime.