← All fixes
Next.js: Module not found: Can't resolve 'fs'
#112Error
Module not found: Can't resolve 'fs'
Why it happens, and why this works
Code that uses Node-only modules (fs, path, child_process, a database driver) ended up in a client bundle: it's imported from a file with "use client", or from a module that client code also imports.
Fix
Keep Node-only code in server files: Server Components, Route Handlers (app/api/.../route.ts), Server Actions, or getServerSideProps in the pages router. Pass the data to the client component as props. Add `import 'server-only'` at the top of such modules so a wrong import fails loudly. Don't 'fix' it with a webpack fallback; the code still can't run in the browser.
Did it work?
No reports yet0 worked0 didn't
Tried it yourself?
Related fixes
Named export not found: the requested module is a CommonJS module
#27SyntaxError: Named export 'greet' not found. The requested module './lib.cjs' is a CommonJS module, which may not support all module.exports as named exports.
nodeVerifiedNo reports yet
ERR_MODULE_NOT_FOUND for a relative import without the .js extension (ESM)
#25Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/utils' imported from /app/app.js. Did you mean to import "./utils.js"?
nodeVerifiedNo reports yet
node --test <folder>/ fails with Cannot find module on Node 22+
#17Error: Cannot find module '/path/to/project/tests' (MODULE_NOT_FOUND) when running node --test tests/
nodeVerifiedNo reports yet