Skip to content
Troubleshooting⏱️ 10 min read·

“Module not found: Can’t resolve ‘fs’” in Next.js: Why It Happens and How to Fix It

Fix the classic Next.js App Router error 'Module not found: Can't resolve fs'. Understand the boundary between Server Components and Client Components, and learn where file operations belong.

MN
Manoj NeupaneSoftware Developer & Programming Instructor
#Next.js#App Router#Node.js#fs#Webpack#Debugging
🎯 Who This Tutorial Is For

📋 Prerequisites
  • ✓A Next.js 13, 14, or 15 application using App Router or Pages Router

🚀 What You Will Learn & Build

  • ▸Why Node.js modules like 'fs' cannot run inside the user's web browser
  • ▸The difference between Server Components and Client Components ('use client')
  • ▸How to move file operations to Route Handlers or Server Components safely
1

Why Browsers Cannot Import 'fs'

Node.js's `fs` (file system) module interacts with physical hard drives. For obvious security and architectural reasons, **web browsers cannot access local file systems**. In Next.js App Router: - **Server Components (default):** Run only on the server. You can freely import `fs`, `path`, and server libraries. - **Client Components (`'use client'`):** Are bundled by Webpack and sent to the browser. If you import `fs` into a component marked with `'use client'`, Webpack fails with: ```bash ./src/components/MyComponent.tsx Module not found: Can't resolve 'fs' ```
2

The Correct Architectural Fix

### Fix 1: Keep 'fs' in a Server Component Remove `'use client'` if your component does not use React hooks (`useState`, `useEffect`) or click listeners: ```tsx // app/blog/[slug]/page.tsx (Server Component by default) import fs from "fs"; import path from "path"; export default function BlogPostPage() { const content = fs.readFileSync(path.join(process.cwd(), "content.txt"), "utf-8"); return
{content}
; } ``` ### Fix 2: Move File Logic to a Route Handler If your client component needs file data, create a Server Route Handler: ```typescript // app/api/file/route.ts import { NextResponse } from "next/server"; import fs from "fs"; export async function GET() { const data = fs.readFileSync("my-file.json", "utf-8"); return NextResponse.json(JSON.parse(data)); } ``` And fetch it from your client component with `fetch('/api/file')`.

✨ Expected Output & Verification

A cleanly decoupled Next.js application where server-only operations execute safely on Node.js without browser bundle errors.

🧪 How to Test the Implementation

  1. 1Inspect the file throwing the error: check if it has 'use client' at the top
  2. 2Move all Node.js built-ins (fs, path, child_process) to server-only files
  3. 3Re-run 'npm run dev' and confirm build compiles without Webpack errors

⚠️ Common Errors & How to Fix Them

❌Adding webpack fallback: { fs: false } in next.config.js blindly

Root Cause: Masks the error by providing an empty stub, but calls to fs.readFile will still fail at runtime with 'fs.readFile is not a function'.

Fix: Do not mock fs. Move the logic to a Server Component or Route Handler.

🛡️ Security Considerations & Best Practices

  • !Never allow user-supplied strings directly into fs.readFileSync to prevent Path Traversal vulnerabilities.

❓ Frequently Asked Questions (FAQ)

Yes! Run 'npm install server-only' and add 'import "server-only"' at the top of any file using 'fs'. Next.js will give you a clear build-time error if imported into a client component.

📌 Summary of Key Takeaways

Node built-ins like 'fs' cannot execute in the browser. Move file system operations to Next.js Server Components or API Route Handlers.

Official References & Further Reading