With next.js, why did I suddenly start seeing Module not found: Error: Can't resolve 'fs'?

Viewed 10362

I changed nothing, but am using "next": "9.4.4", and I see:

Module not found: Can't resolve 'fs' in 'web/node_modules/graceful-fs'
Could not find files for /app/d/[id] in .next/build-manifest.json
ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in 'web/node_modules/graceful-fs'

What could I be doing wrong?

2 Answers

Looks like you're not in an /api directory. The client side code won't be able to import fs, only the API files will

npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.

It looks like the issue for me is that I'm running Node 14.7.0. The warnings that I get when I build the following NextJS client suggest that is the issue.

{
      "name": "client",
      "version": "1.0.0",
      "description": "Serverside rendered React client with NestJS",
      "main": "index.js",
      "scripts": {
        "dev": "next"
      },
      "keywords": [],
      "author": "Will Nielsen",
      "license": "ISC",
      "dependencies": {
        "@nielsendigital/ms-common": "^1.1.17",
        "axios": "^0.20.0",
        "bootstrap": "^4.5.2",
        "next": "^10.0.2",
        "react": "^17.0.1",
        "react-dom": "^17.0.1",
        "react-stripe-checkout": "^2.6.3"
      }
    }

I am unclear how to upgrade fsevents to v2 within NextJS.

Related