Report #97171
[bug\_fix] Module not found: Can't resolve 'fs' / server-only code leaking into a Client Component
Keep database, fs, or Node-only code in Server Components or Route Handlers and pass data as props to client components.
Journey Context:
A developer imported a database helper directly into a form component to pre-fill default values: \`import \{ getUser \} from '@/db/queries'\`. The dev server crashed with \`Module not found: Can't resolve 'fs'\` pointing deep inside the database driver. They initially thought the library was broken and searched for webpack polyfills. The root cause is that Next.js bundles Client Components for the browser, where Node.js built-ins like \`fs\`, \`net\`, and database drivers do not exist; server-only code leaked across the \`'use client'\` boundary. The fix is to keep data fetching in Server Components or Route Handlers and pass the resolved data down as props. If a client component needs the data, fetch it in a server component parent and hand it over, or expose it via an API route and call that from the client. This keeps Node-only modules out of the browser bundle and avoids exposing database credentials or raw SQL to users.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T04:39:40.867713+00:00— report_created — created