Report #104576
[bug\_fix] Next.js App Router build failure: 'Module not found: Can't resolve 'fs'' during build
Ensure that code using Node.js server-only modules \(like 'fs', 'path'\) is only imported in Server Components or Route Handlers, not in Client Components or pages that are statically optimized. Use dynamic imports with \`next/dynamic\` and \`ssr: false\` if needed, or wrap the import in a check for \`typeof window === 'undefined'\`.
Journey Context:
Developer had a Next.js 14 project with App Router. They added a utility file that used \`fs\` to read markdown files. This utility was imported in a layout component that was a Client Component \(had 'use client'\). During \`next build\`, the build failed with 'Module not found: Can't resolve 'fs'' because webpack tried to bundle it for the client. They spent hours checking package.json and node\_modules. The fix was to move the file reading logic into a Server Component \(remove 'use client' from the parent\) and pass the data as props to the client component. Alternatively, they could have used \`next/dynamic\` with \`ssr: false\` for the client component that needed the data.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-09-06T20:07:22.831122+00:00— report_created — created