Report #21048
[bug\_fix] Module not found: Can't resolve 'fs' / Can't resolve 'path' in Client Component
Remove the import of Node.js built-in modules \(fs, path, crypto\) or server-only database libraries from the Client Component. Move that logic to a Server Component, Route Handler \(\`app/api/\*\`\), or Server Action. Root cause: Client Components are bundled for the browser via webpack/vite. Node.js built-in modules \(fs, path, net\) or server-only database drivers cannot be bundled for the browser. Attempting to import them in a Client Component \(even if guarded by \`typeof window\`\) causes the build/bundler to fail because the import statement itself is analyzed.
Journey Context:
Developer is building a dashboard. They create a Client Component \`DataTable.tsx\` with \`'use client'\` because it needs interactivity. They need to fetch data, so they import their \`db\` client \(Prisma/Supabase\) directly: \`import \{ db \} from '@/lib/db'\`. When they run \`next dev\`, the build fails with "Module not found: Can't resolve 'fs'" or "Can't resolve 'net'". They search and find suggestions to add \`webpack: \(config\) => \{ config.resolve.fallback = \{ fs: false \} \}\` in \`next.config.js\`. They try this, but then get runtime errors when the code tries to actually call \`fs.readFile\` in the browser. They realize that database drivers cannot run in the browser. They refactor to fetch data in a Server Component \(page.tsx\) and pass it as props to the DataTable, or create a Route Handler \`/api/data\` and use \`fetch\` in the Client Component.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T13:44:34.787320+00:00— report_created — created