Skip to content

Next.js build error

Next.js: “Module not found: Can’t resolve ‘fs’”

The build fails with Module not found: Can’t resolve fs, or path, net, tls or child_process. Those are Node.js modules that exist on the server but not in the browser, and something in the client bundle imports them. For example, a database client, an SDK or a helper file imported into a component marked "use client". Here’s how to find and fix it, in Next.js’s words, or hand it over at a fixed price.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

A file in the client bundle imports a Node.js module. Follow the import trace. In app/, move Node.js work to a Server Component, a Server Action or a Route Handler on the Node.js runtime. In pages/, use getStaticProps, getServerSideProps or an API route. Pass the result to the component. Add import "server-only" to server files so the next mistake fails clearly.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

How server code ends up in the browser

  1. 01

    A Client Component imports server code

    “Once a file is marked with "use client", all of its imports and the components it directly renders are included in the client bundle.” Next.js, Server and Client Components

    Everything a "use client" file imports goes to the browser. If it imports your database client or a file that reads from disk, the build tries to bundle fs. Move that call to the server side and pass the result down as props.

  2. 02

    A shared helper file mixes both

    “This means it's possible to accidentally import server-only code into the client.” Next.js, Server and Client Components

    A utils file with both a formatting helper and a database call gets pulled into the client by the formatting helper. Split it into a client-safe file and a server file.

  3. 03

    Mark server files with server-only

    “To prevent accidental usage in Client Components, you can use the server-only package.” Next.js, Server and Client Components

    Import it at the top of each server file, as below. If a Client Component then imports that file, the build fails with a clear error pointing at the mistake. In Next.js, installing the server-only package is optional; install it (npm install server-only) if your lint rules flag undeclared dependencies.

    import 'server-only'
  4. 04

    In the Pages Router, outside getServerSideProps

    “The module you're trying to import uses Node.js specific modules, for example dns, outside of getStaticProps / getStaticPaths / getServerSideProps” Next.js, Module Not Found

    In pages/, keep file reads in getStaticProps, getStaticPaths, getServerSideProps or a Node.js API route under pages/api/. The page component itself must also run in the browser.

Checked against the vendor’s docs on : Next.js docs: Server and Client Components; Next.js docs: Module not found.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Vercel Module not found: Can’t resolve (casing) · Next.js window is not defined · Vercel npm run build exited with 1 · Next.js Edge Runtime does not support Node.js module

Questions

More on this error

Should I add a webpack fallback for fs?

Setting fs to false hides the error, but the code that needed fs still won’t work in the browser. Move it to the server instead.

Is this the same as the casing error on Vercel?

No. A casing mismatch names your own file. This error names a Node.js built-in module, which never exists in the browser.

Can you fix it for me?

The optional Diagnosis costs $450. The Fix is $1,200 in total. If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.