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
- Keep fs usage in server components, route handlers or getServerSideProps
- Split server-only code into separate modules (import 'server-only')
- Use browser alternatives or webpack fallbacks when appropriate
Detailed fix by platform
React
- jsx
// lib/db.ts import 'server-only';
How to diagnose
- Import chain — Which client module imports it?
- 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026