Can't resolve 'fs' ⚛️ React

Module not found: Can't resolve 'fs' (Next.js / webpack 5)

A Node.js built-in (fs, path, crypto, net) is imported into code that runs in the browser bundle.

Seen on: React

Meaning

Webpack 5 no longer polyfills Node built-ins. Server-only code imported by a client component or a Node-only library used in the browser breaks the build.

Common causes

  • Importing server code (fs/db clients) into a client component
  • Node-only library used client-side
  • Shared utils importing fs

⚡ Quick fix

  1. Keep fs usage in server components, route handlers or getServerSideProps
  2. Split server-only code into separate modules (import 'server-only')
  3. Use browser alternatives or webpack fallbacks when appropriate

Detailed fix by platform

React

  1. jsx
    // lib/db.ts
    import 'server-only';

How to diagnose

  1. Import chain — Which client module imports it?
  2. Library — Node-only?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.