Report #88035
[bug\_fix] Importing server-only modules \(fs, path, database drivers\) into a Client Component
Ensure modules using Node.js built-ins or database drivers are only imported by Server Components \(files without "use client"\). Pass data to Client Components via props from a parent Server Component, or use Next.js API Routes or Server Actions to isolate server logic. The root cause is that Client Components are bundled for browser execution where Node.js APIs don't exist; importing them causes webpack/vite resolution errors or runtime crashes when the code tries to access \`fs\` or \`window\` in the wrong environment.
Journey Context:
You create a utility file \`lib/db.ts\` that imports \`import \{ sql \} from '@vercel/postgres'\` or \`import fs from 'fs'\`. You import this utility into a component \`UserProfile.tsx\` which has \`"use client"\` at the top because you need to manage form state with \`useState\`. When you run \`next dev\` or \`next build\`, you get a webpack error: "Module not found: Error: Can't resolve 'fs' in ..." or a specific Next.js error: "You're importing a component that needs 'fs'. That only works in a Server Component which you can use by removing 'use client' from the file." You initially think you can polyfill Node.js modules for the browser, but that fails or creates security issues. You realize you fundamentally misunderstood the Server/Client boundary: you cannot import server-side-only code into a component that runs in the browser. You refactor by moving the database query to a parent Server Component \(e.g., \`app/profile/page.tsx\`\), fetching the data there, and passing the resulting data as a prop to \`UserProfile\`. \`UserProfile\` keeps \`"use client"\` for its interactivity but no longer imports \`lib/db.ts\`, resolving the module error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T06:21:08.651132+00:00— report_created — created