# Module not found: Can't resolve 'fs' (Next.js)

Printed by next build or the dev server while bundling for the browser, followed by an import trace from a client file to the module that needs fs. Node built-ins such as fs, net, tls and child_process do not exist in browser bundles.

```
Failed to compile.

./lib/posts.ts:2:1
Module not found: Can't resolve 'fs'
  1 | import matter from 'gray-matter';
> 2 | import fs from 'fs';
    | ^
  3 | import path from 'path';

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./components/post-list.tsx
./app/blog/page.tsx
```

## Quick fix

Follow the import trace under the error to the first file marked 'use client' (or a Pages Router component) and cut the chain there. Keep fs usage in Server Components, route handlers, server actions or getServerSideProps, and add import 'server-only' to that module so a future client import fails clearly.

1. Read the import trace printed below the error.
2. Find the first file in the trace that is a client component.
3. Move the fs call into a server-side module and pass the result down as props.
4. Split barrel files (index.ts) that export both server and client helpers.
5. Add import 'server-only' at the top of modules that touch the filesystem.

```ts
// lib/posts.ts: server only
import 'server-only';
import { readFile } from 'node:fs/promises';

export async function getPost(slug: string) {
  if (!/^[a-z0-9-]+$/.test(slug)) throw new Error('invalid slug');
  return readFile(`content/${slug}.md`, 'utf8');
}

// app/blog/[slug]/page.tsx: Server Component (no 'use client')
import { getPost } from '@/lib/posts';
import { PostBody } from './post-body'; // client component gets plain text

export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  return <PostBody markdown={await getPost(slug)} />;
}
```

## Why it happens

### A client component imports server code

A file marked 'use client' imports a helper that reads files: a config loader, a markdown reader, a logger with a file transport. Everything a client file imports is bundled for the browser, fs included.

### Barrel files mix server and client exports

lib/index.ts re-exports a pure formatting helper and a function that uses fs. Importing only the harmless helper through the barrel still makes the bundler resolve the whole file, and module resolution happens before tree shaking can drop anything.

### Node-only package used from the browser

Packages written for Node (dotenv, database drivers, many server SDKs, file-based loggers) get pulled into a client component. Check whether the package ships a browser build; if not, call it from the server.

### Pages Router: fs outside data functions

In pages/, Next strips fs from the client bundle only when it is used inside getServerSideProps or getStaticProps. Using it in the component body, or in a helper the component also calls, keeps it in the browser bundle.

### A library guards fs at runtime

Some packages require('fs') behind a check that never runs in the browser. The bundler still has to resolve the import, so these need a resolve fallback or alias rather than moving your code.

## Other fixes

- For a library that only touches fs behind a server check, set resolve.fallback = { fs: false } for client builds in the webpack config.
- Under Turbopack, alias the module for browser builds with turbopack.resolveAlias, for example fs: { browser: './empty.ts' }.
- Swap the Node-only package for one with a browser build, or call it through a route handler.

## Same error, other wording

- `Module not found: Error: Can't resolve 'fs' in '/app/node_modules/x/lib'`
- `Module not found: Can't resolve 'child_process'`
- `Module not found: Can't resolve 'net'`
- `UnhandledSchemeError: Reading from "node:fs" is not handled by plugins (Unhandled scheme).`

## Sources

- [Next.js: Module Not Found](https://nextjs.org/docs/messages/module-not-found)
- [Next.js: Server and Client Components](https://nextjs.org/docs/app/getting-started/server-and-client-components)

---

This error stops the build, before your app runs, so Runheed doesn't record this one. Runheed records the exceptions a running app throws and serves them to coding agents over MCP: https://runheed.com/?ref=agent-md

Human-readable version: https://runheed.com/errors/module-not-found-fs
