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

← Next.js answers

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.
// lib/read-content.ts
import "server-only";
import { readFile } from "node:fs/promises";
 
export const readContent = (name: string) => readFile("content/" + name, "utf8");

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required