When updating Next.js v10.1.13 to webpack5, getting warnings Can't resolve 'fsevents' in chokidar

700 Software picture 700 Software · Apr 13, 2021 · Viewed 9.5k times · Source

npm install next react react-dom And running Node.js v12

Created most simple pages/index.tsx

export default function PageHome(props) {
  return <>Hello World!</>
}

(I also had TypeScript configured as per Next.js instructions but not sure if that's making a difference.)

C:\GitHub\reproduce-nextjs-webpack5-error>npm run dev
...
event - compiled successfully
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'fsevents' in 'C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar'
<w> while resolving 'fsevents' in C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar to a directory
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'fsevents' in 'C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar'
<w> while resolving 'fsevents' in C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar to a directory

Self-answered solution below for future readers.

Answer

700 Software picture 700 Software · Apr 13, 2021
  1. Upgrade Node.js

  2. Delete package-lock.json and node_modules

  3. Run npm install again

  4. It works

    • Apparently something doesn't install when you run with an older Node.js version.

    • My package.json looked like

      {
        "scripts": {
          "dev": "next dev",
          "build": "next build",
          "start": "next start"
        },
        "dependencies": {
          "next": "^10.1.3",
          "react": "^17.0.2",
          "react-dom": "^17.0.2"
        },
        "devDependencies": {
          "@types/react": "^17.0.3",
          "typescript": "^4.2.4"
        }
      }
      
    • The same package.json will install slightly differently switching from Node.js v12 to Node.js v15 as I just observed. This is why you have to complete not just step 1, but also steps 2 & 3.

Before:

C:\GitHub\reproduce-nextjs-webpack5-error>node --version
v12.4.0

C:\GitHub\reproduce-nextjs-webpack5-error>npm run dev

> @ dev C:\GitHub\reproduce-nextjs-webpack5-error
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
event - compiled successfully
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Can't resolve 'fsevents' in 'C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar'
<w> while resolving 'fsevents' in C:\GitHub\reproduce-nextjs-webpack5-error\node_modules\next\node_modules\chokidar to a directory

After:

C:\GitHub\reproduce-nextjs-webpack5-error>node --version
v15.14.0

C:\GitHub\reproduce-nextjs-webpack5-error>npm run dev

> dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
event - compiled successfully
event - build page: /
wait  - compiling...
event - compiled successfully

Yay! No warnings!

While there are comments below about future changes in nextjs@canary, the above examples worked for me with versions listed.