Report #104730
[bug\_fix] Client component importing server-only module \(like fs\) causing build error
Ensure server-only modules are not imported in client components; use \`import 'server-only'\` in server files and avoid importing them in client bundles.
Journey Context:
A developer tried to read a file from the filesystem inside a client component using \`const fs = require\('fs'\)\` \(or \`import fs from 'fs'\`\). During build, Next.js threw an error: 'Module not found: Can't resolve 'fs''. The developer thought it was a missing dependency, but \`fs\` is a Node.js built-in that is not available in the browser. They then moved the file-reading logic to a server component and passed the result as a prop to the client component. They also learned about the \`server-only\` package, which can be used to mark server-only code and enforce separation. The official Next.js documentation on server-only and client-only packages explains this pattern. The root cause is that client components are bundled for the browser, which does not have Node.js modules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-10-04T20:04:59.710949+00:00— report_created — created