Fix guides · Cursor, Claude Code, v0, Bolt, Windsurf ·

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

Module not found: Can't resolve 'fs'

"Can't resolve 'fs'" means code that only works on a server (reading files, Node built-ins like fs, path or crypto) ended up in the browser bundle. In Next.js this happens when a Client Component ('use client') imports that code, directly or through another file, or when a package that needs Node is imported in client code. The fix is to keep that code on the server, not to fake the fs module.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. A Client Component imports server code

How to tell: The import trace runs through a file with 'use client' at the top, and further down a helper that uses fs, path or a database client.

Fix: Move the file reading into a Server Component, a Route Handler or a server action, and pass only the result to the Client Component as props.

2. A barrel file mixes server and client exports

How to tell: The client component imports something harmless from '@/lib' or '@/utils', and that index.ts also re-exports a module that uses fs.

Fix: Import directly from the specific file (e.g. '@/lib/format') or split the barrel into a client-safe and a server-only part.

3. A Node-only package is used in the browser

How to tell: The trace ends inside node_modules (for example a PDF, Excel, markdown or SDK package that reads files), imported from a client component.

Fix: Call that package from a Route Handler or server action and fetch the result, or use the package's browser build if it has one.

4. Middleware runs on the Edge runtime

How to tell: The error points to middleware.ts or a helper it imports. The Edge runtime has no file system.

Fix: Keep middleware light (cookies, redirects) and move file or database work into a route handler. Newer Next.js versions can run middleware/proxy on Node.js. Check the docs for your version before relying on it.

5. Pages Router: server code imported in a page component

How to tell: The project uses pages/ and the fs code is imported at the top of a page but used in the component body or in a helper shared with the component.

Fix: Use fs only inside getStaticProps or getServerSideProps, or in API routes, and keep shared helpers free of Node imports.

How to fix it

  1. Read the Import trace below the error: it lists the chain from your page or component down to the file that imports fs.
  2. Find the first file in that chain that has 'use client' at the top. That's where server code leaks into the browser.
  3. Move the fs (or database, or Node-only) code into a Server Component, Route Handler or server action, and pass the data as props.
  4. Check barrel files (index.ts that re-export many modules). Import client-safe things directly instead of through a barrel that also exports server code.
  5. Add import 'server-only' at the top of server-only helper files, so Next.js gives a clear error if a Client Component imports them again.
  6. Run next build and confirm the error and the import trace are gone.

Using Lovable, Bolt or v0?

In v0 or Bolt, paste the full error including the import trace and say: "This code reads files on the server. Keep it out of 'use client' components: load the data in a server component or API route and pass it down as props."

Using Cursor, Claude Code or Windsurf?

Ask Cursor or Claude Code to run next build, follow the import trace from the 'use client' file to the fs import, and move the server logic behind a Server Component or Route Handler. Have it add import 'server-only' to server helpers so the mistake fails loudly next time.

The “fix” to avoid

Agents often add resolve.fallback = { fs: false } to the webpack config or install the old 'fs' npm package. That only hides the problem: the code still can't read files in the browser and breaks at runtime. With Turbopack the webpack fallback doesn't even apply.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My Next.js app fails with "Module not found: Can't resolve 'fs'". Read the import trace and find the first file in the chain with 'use client'. Server-only code (fs, path, database clients, Node-only packages) must not be imported from Client Components. Move that code into a Server Component, Route Handler or server action and pass the data as props. Check barrel index files that re-export server code. Add import 'server-only' to server helper files. Don't add webpack fallbacks like fs: false or install a fake 'fs' package. Run next build and show me the result.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Can I just set fs: false in next.config?

It silences the build for webpack, but the code that needs fs will still fail when it runs in the browser. Only use it if a package imports fs without actually using it in the browser, and prefer moving the code to the server.

Does the same happen with 'path', 'crypto' or 'child_process'?

Yes. Any Node built-in in client code gives the same "Can't resolve" error. The fix is the same: keep that code on the server.

What does import 'server-only' do?

It marks a file as server-only. If a Client Component imports it, Next.js fails the build with a clear message that names the problem, instead of a confusing module error.

Official documentation

Related errors

Get fixes like this every week