Module not found: Can't resolve 'fs'
Last verified:
The fix
Module not found: Can't resolve 'fs': client code imported fs and Next.js tried to bundle it. Move the import to a Server Component or route handler.
- 1
Read the import trace from the bottom up
The error lists an import trace for the requested module. The last entries are your files. Open the first file in that list that you wrote, and find the import that pulled in
fs,path,child_process, or a package that uses them. That package might be a database client, a mailer, ornext/serverre-exported through a barrel file. - 2
Move the Node code to a server file
Put the
fscall in a Server Component, a route handler, or a server action. Pass plain data into the client component as props. Do not add"use client"to the file that importsfs. Do not import the server file from a client file. If a server subtree has to appear inside a client component, pass it aschildrenfrom a server parent.import "server-only"; import fs from "node:fs"; export function readCopy() { return fs.readFileSync("content/copy.txt", "utf8"); } - 3
Mark the server module so the next import fails loudly
Add
import 'server-only'at the top of the module that touches the filesystem or a secret. The data-security guide says this causes a build error if the module is imported into the client. Install theserver-onlypackage only if lint complains that it is missing. Next.js does not use the package's npm contents. - 4
Do not stub fs out in webpack
Do not paste
webpack: (config) => { config.resolve.fallback = { fs: false }; return config }as the fix. The client bundle then loads, and the call throws at runtime because there is still no filesystem. Change the fallback only if that library's own documentation says the browser build must ignorefs.
Why it happens
Node's fs, path, and child_process modules exist on the server. They are not in the browser bundle. In the App Router, a file is a Server Component until it or something that imports it is marked "use client". Once a file is in the client module graph, its imports are bundled for the browser, including a library that quietly imports fs. The compiler then reports Module not found: Can't resolve 'fs' and prints an import trace back to your file.
Next.js documents import 'server-only' for this boundary. The import fails the build if a Client Component pulls that module in. Next.js handles the server-only import internally, so installing the npm package is optional unless your lint rules want it listed. Setting webpack resolve.fallback to { fs: false } hides the missing module. It does not give the browser a filesystem. Use that only when the library you imported documents it.
The next error you'll probably hit
Once this is fixed, the usual follow-up is useState only works in a Client Component. useState only works in a Client Component. This Next.js file is a Server Component until the first line is use client. Add that, or move the hook.
Prompt to paste back into your AI tool
Paste this above the error.
Next.js reports: Module not found: Can't resolve 'fs' (or 'path' or 'child_process'). Here is the import trace: <paste the trace> Do not set webpack resolve.fallback fs to false unless the library's own docs say to. Do not add "use client" to the file that imports fs. Do not disable RLS. Do not put a secret in NEXT_PUBLIC_ or VITE_. Do not swallow the error in an empty catch. 1. Name the first project file in the import trace and the import that pulls in the Node module. 2. Move that code into a Server Component, a route handler, or a server action. Pass serializable props to the client. 3. Add import 'server-only' at the top of the server module so a later client import fails the build. 4. Show only those files.
Common questions
+The import of fs is inside node_modules, not my file. What do I change?
The trace still ends at your import of that package. Import the package from a server file only. If the package has a separate browser entry, use the entry its docs name. Do not alias fs to an empty module to silence the trace.
+I added server-only and the build says it cannot be imported from a Client Component. Did that work?
Yes. That build error is the guard. Remove the import from the client file and call the server module from a Server Component, a route handler, or a server action instead.
Related errors
- useState only works in a Client ComponentuseState only works in a Client Component. This Next.js file is a Server Component until the first line is use client. Add that, or move the hook.Cursorv0BoltNext.jsReactNov 11, 2026
- Module not found: Can't resolveModule not found: Can't resolve means the import path or package is not on disk, often after an AI edit. Fix the path or install the package, then restart.CursorLovableBoltTypeScriptNext.jsOct 14, 2026