Report #103728
[bug\_fix] Build error: 'Module not found: Can't resolve 'fs' in ...' when using Node.js built-in modules in a browser bundle
Do not import Node.js built-in modules \(like \`fs\`, \`path\`, \`os\`\) in code that runs on the client. Use them only in Server Components, Route Handlers, or \`getServerSideProps\`/\`getStaticProps\` in the Pages Router. The root cause is that Webpack or Turbopack tries to bundle these modules for the client, where they don't exist. The established fix is to ensure the import is only used in server-side code, or use dynamic imports with \`typeof window === 'undefined'\` checks.
Journey Context:
I was building a Next.js 13 app that needed to read a local JSON file for static data. I imported \`fs\` and \`path\` in a component that was used in the App Router. The build failed with: 'Module not found: Can't resolve 'fs''. I spent hours checking my \`next.config.js\` and trying to polyfill \`fs\`. The real issue was that the component was a Client Component \(or was imported into a client boundary\), and Webpack tried to bundle \`fs\` for the browser. The fix was to move the file-reading logic into a Server Component or a Route Handler, and pass the data as props. I created a \`getData\` function in a separate file and called it only in Server Components. This is a fundamental constraint of the App Router's server/client architecture.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-12T20:08:45.645801+00:00— report_created — created