---
title: "How do you fix \"Module not found: Can't resolve 'fs'\" in Next.js?"
description: "The fs module exists only in Node.js. Keep fs imports on the server, move them out of client components, or stub them for the browser bundle."
url: "https://bugsee.com/answers/nextjs/fix-module-not-found-cant-resolve-fs/"
---

# How do you fix "Module not found: Can't resolve 'fs'" in Next.js?

A client-side file imported `fs`, which only exists in Node.js. Move the `fs` code into server-only code, such as `getStaticProps`, a Route Handler or a Server Component.

The browser bundle cannot include Node built-ins like `fs`, `path` or `net`. The error appears when a file that ends up in the client bundle imports one, directly or through a library.

- Move the file reading into server code and pass the result to the component as props.
- In the App Router, add `import "server-only"` to modules that must never reach the browser; the build then points at the exact client import that pulls it in.
- In the Pages Router, imports used only inside `getStaticProps` or `getServerSideProps` are removed from the client bundle.
- As a last resort for a library with an optional `fs` call, alias `fs` to an empty module for the browser: `turbopack.resolveAlias` in `next.config.js` (the default bundler in Next.js 16), or `resolve.fallback = { `fs`: false }` in a webpack config if you build with `--webpack`.

```ts
// lib/read-content.ts
import "server-only";
import { readFile } from "node:fs/promises";

export const readContent = (name: string) => readFile("content/" + name, "utf8");
```

## Related questions

- [How do you fix "window is not defined" in Next.js?](https://bugsee.com/answers/nextjs/window-is-not-defined/)
