Next.js 15: Module not found: Can't resolve 'fs' in Client Component
Module not found: Can't resolve 'fs' in '/app/src/components/FileReader.tsx'
> 1 | import fs from 'fs';
2 | export default function FileReader() { return <div>{fs.readFileSync('/etc/passwd')}</div> }
Immediate Remediation
bash
Remove the fs import from the client component and move file operations to a server action or API route. If you must read files on the client, use a server component wrapper or fetch from an API endpoint.
Root Cause Analysis
The 'fs' module is a Node.js built-in that is not available in the browser. When a component is marked as a Client Component (via 'use client' or being imported into one), Next.js bundles it for the browser, and any Node.js-specific modules like 'fs' cannot be resolved, causing a build-time error.
Verification & Guardrails
- Ensure the component is not accidentally imported into a client component; check for 'use client' directives in the file or its parents.
- For server-side file access, use Server Components (no 'use client') or Server Actions, which run on the Node.js runtime.
- If you need to read files at runtime on the client, consider fetching from a custom API route that runs on the server.
Have a custom or uncategorized crash?
Run your trace through our in-memory client privacy sandbox for instant SRE remediation.