Report #4867
[bug\_fix] Module not found: Can't resolve 'fs' \(or 'path', 'crypto'\) in a Client Component
Move the server-only logic \(using fs, database drivers, etc.\) into a Server Component \(page.tsx or a separate async component\) and pass the fetched data as props to the Client Component, or use a Route Handler \(API route\) if the client needs to fetch data dynamically.
Journey Context:
Developer creates a utility \`lib/db.ts\` that imports \`fs\` to read a local JSON file. They import this utility into a Client Component marked with 'use client' to display the data. Upon running \`next build\`, Webpack throws "Module not found: Can't resolve 'fs'" because it's trying to bundle the Node.js 'fs' module for the browser environment. Developer initially tries to add webpack config to ignore fs, but that breaks the actual functionality since the code needs fs. They realize the architectural mistake: 'use client' components are bundled for the browser, so they cannot use Node.js built-ins. The solution is to move the data fetching \(fs.readFile\) into the Server Component \(page.tsx\), which runs only on the server during build or request. The Server Component reads the file and passes the data as a prop to the Client Component. Developer refactors: page.tsx becomes an async function that reads the JSON, then renders the \`\`. The Client Component receives the data via props and renders it without needing fs. Build succeeds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:12:45.209968+00:00— report_created — created