Report #76264
[bug\_fix] Module not found: Can't resolve 'fs' \(or 'path', 'child\_process'\) in client component
Move the server-only logic \(file system, database calls\) to a Server Component \(default in App Router\) and pass the resulting data as props to the Client Component. Alternatively, use a Server Action for mutations.
Journey Context:
Developer creates a Client Component \('use client'\) in Next.js App Router that tries to read a local file using fs.readFileSync or queries a database directly. On build, webpack fails with 'Module not found' for Node.js built-ins like fs or path because it's trying to bundle server code for the browser. Developer tries adding webpack config to polyfill fs with false or empty object, which silences the error but breaks runtime when the code actually runs in the browser where fs doesn't exist. They try dynamic imports with ssr: false but the import is at the top level. The fix is refactoring: keep the data fetching in a Server Component \(default in app directory\) which has access to Node.js APIs, then pass the data as props to a Client Component that handles interactivity. This works because App Router separates server and client boundaries; Server Components execute exclusively in the Node.js runtime during SSR/build with full server access, while Client Components are browser-bound and must not import server-only modules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T10:35:54.045241+00:00— report_created — created