turborepo: Error when building nextjs 15 app while prerendering /500 & /404 (only happens in monorepo)

Verify canary release

  • I verified that the issue exists in the latest Turborepo canary release.

Link to code that reproduces this issue

https://github.com/ezeamin/turbo-next-15

What package manager are you using / does the bug impact?

npm

What operating system are you using?

Windows

Which canary version will you have in your reproduction?

turbo@2.2.4-canary.2

Describe the Bug

When minimal nextjs app (15.0.1 & 15.0.2-canary.6 - latest up to today) is being built, these errors appear:

┌ home#build > cache miss, executing 570f9f11bf8e8c82
│
│ > home@0.0.1 build
│ > next build
│
│   ▲ Next.js 15.0.1
│
│    Creating an optimized production build ...
│  ✓ Compiled successfully
│  ✓ Linting and checking validity of types
│  ✓ Collecting page data
│ Error: Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%24%24typeof%2C%20type%2C%20key%2C%20ref%2C%
│ 20props%7D for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:490)
│     at Xc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:68:409)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:70:210)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:89)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:70:481)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:89)
│     at $c (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:78:98)
│     at bd (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:77:404)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:217)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:71:479)
│ Error occurred prerendering page "/404". Read more: https://nextjs.org/docs/messages/prerender-error
│ Error: Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%24%24typeof%2C%20type%2C%20key%2C%20ref%2C%
│ 20props%7D for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:490)
│     at Xc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:68:409)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:70:210)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:89)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:70:481)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:89)
│     at $c (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:78:98)
│     at bd (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:77:404)
│     at Z (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:76:217)
│     at Zc (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\node_modules\react-dom\cjs\react-dom-server.browser.production.min.js:71:479)
│ Export encountered an error on /_error: /404, exiting the build.
│  ⨯ Static worker exited with code: 1 and signal: null
│ npm error Lifecycle script `build` failed with error:
│ npm error code 1
│ npm error path C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\apps\home
│ npm error workspace home@0.0.1
│ npm error location C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\apps\home
│ npm error command failed
│ npm error command C:\WINDOWS\system32\cmd.exe /d /s /c next build
│ command finished with error: command (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\apps\home) C:\Program Files\nodejs\npm.cmd run build exited (1)
└────>
home#build: command (C:\Users\Amin\Documents\Codigo\Pruebas\turbo-next-15\apps\home) C:\Program Files\nodejs\npm.cmd run build exited (1)

This may seem a nextjs error, but it doesn’t happen outside of a monorepo handled by turborepo.

It sometimes shows error while prerendering /404, and sometimes while prerendering /500. This WASN’T happening with nextjs@14. This is shown when “Generating static pages (0/4)” is run, after other stages had finished successfully…

Dev execution runs without problems. Running npm run build from apps/home also shows the same errors.

It also occurs with bun@1.1.33, tested on linux and windows.

Expected Behavior

Build finishes without any problems.

To Reproduce

  1. Run npm i --force (to avoid react@19 peer deps errors)
  2. Run npm run build
  3. See console output.

Additional context

I tested this on different PCs, using next 15,15.0.1, and over canary release 15.0.2-canary.6. Also tested on docker.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 35
  • Comments: 35 (3 by maintainers)

Most upvoted comments

@GavGrayston, @ezeamin et. al… I found a fix for this for my use case - this may help someone else: Note that I’m using npm for my package manager, so your mileage may differ when using pnpm, yarn etc.

After performing npm install from the root folder, my nextjs application (in the apps/web-server sub-folder for me) contained its own node_modules folder which only contained react and react-dom packages. The root node_modules folder also contained the same versions. For some reason, next build seems to be confused by the dueling packages, so I simply deleted my app’s node_modules folder (i.e. apps/web-server/node_modules in my case) and then re-ran the build. Voila! everything worked fine.

This issue is happening also for Nx monorepo (npm workspace) When your react and react-dom at package.json mono repo root version are different from react and react-dom in the package.json in your app/project

The temp fix is to delete react and react-dom in the node_modules in your app/project but this is not viable long run

What happens when you have 2 project, and one must be on a specific different react version ? like an expo mobile app for example

we should be allowed to have different version of a package per project

We need a real fix for this

@GavGrayston - glad that helped

I took a slightly different approach. Instead of removing from the sub-app package.json (which is where the dependencies for react/react-dom should really be located), I created a clean-react turbo task which I include as a dependency of the build task. There’s obviously a corresponding entry in the app’s package.json scripts for clean-react which does a rm -rf node_modules/react; rm -rf node_modules/react-dom (I’m building and deploying for linux so these commands work in that context - should also work on Mac, but not on windows - there’s probably an npm file remover package that will work everywhere).

The reason for mentioning this is that pnpm and yarn use different file linking strategies to share packages (and to ensure isolation of dependencies between packages etc.). Keeping the dependency within the app prevents pollution of the top-level package.json and keeps everything neat and tidy.

@GavGrayston, @ezeamin et. al… I found a fix for this for my use case - this may help someone else: Note that I’m using npm for my package manager, so your mileage may differ when using pnpm, yarn etc.

After performing npm install from the root folder, my nextjs application (in the apps/web-server sub-folder for me) contained its own node_modules folder which only contained react and react-dom packages. The root node_modules folder also contained the same versions. For some reason, next build seems to be confused by the dueling packages, so I simply deleted my app’s node_modules folder (i.e. apps/web-server/node_modules in my case) and then re-ran the build. Voila! everything worked fine.

This worked !!

I am also being affected by prerendering errors on /400 and /500 pages during turbo run build in a monorepo managed by Turborepo 2.3.3, trying to upgrade an app from Next.js 14.2 -> 15.1 and React 18.3 -> 19.0

Removing and regenerating package-lock.json as mentioned by @inadeqtfuturs above has not fixed the errors in my case.

edit: I ended up with a workaround fix of specifying react 19.0.0 and react-dom 19.0.0 in my top-level package.json in addition to my apps/web/package.json file.

Prior to making this change, I only had react and react-dom in my apps/web/package.json and was ending up with a version mismatch caused by two copies of the react and react-dom packages within the monorepo:

  • node_modules/react and node_modules/react-dom contained the 18.3 versions
  • apps/web/node_modules/react and apps/web/node_modules/react-dom contained the 19.0 version

After adding react and react-dom to the package.json in my Turborepo root, I now have:

  • node_modules/react and node_modules/react-dom contain the 19.0 version
  • apps/web/node_modules is empty

thanks @fuzing for pointing me in the right direction of looking at a package version mismatch

I had to add react and react-dom (and their corresponding @types/react to the root package.json in my monorepo. For me, yes it was a type mismatch where the top-level node_modules had React 18 for some reason.

Same issue here. Also in an npm monorepo.

    "turbo": "2.3.3"
    "react": "19.0.0",
    "react-dom": "19.0.0",

I found that the root cause is an npm workspace depending on another, with both having react and react-dom in their package.json.

The solution is to make sure that react and react-dom are only being specified once each, either in the parent or child npm workspace.

Also, is there another issue where this is being discussed? It’s surprising that this issue was closed, and the one in the next.js repository referenced in the comments here as well.

Very cryptic and difficult to debug error.

Can’t believe there’s no official support on this yet - this is such a huge issue, just wasted 3 hours trying to figure out why my nextjs app builds perfectly fine outside of turborepo but gets that misleading error with turborepo… 🤦‍♂️

I had the same issue. I realized that a few of the dependencies in apps/web had older versions of react & react-dom as peer dependencies. My solution was to update them to the versions that have react v19 as a peer dependency and then the error went away.

just to chime in: using npm and turborepo, i got into this state today, found this thread, and started looking at mis-matched react versions in the child (nextjs app) and parent (root turbo repo) node_module folders. i think because i made the transition from next 14 to 15, as well as from react 18 to 19, my root package-lock.json still specified and downloaded an older version of react than what the nextjs app needed/wanted.

while i initially got a build to work by deleting my child’s node_modules as suggested by @fuzing, i ultimately just deleted my root package-lock.json and generated a new one. i’m now in a good state.

@fuzing That worked for me as well, thanks a lot!

Same issue.

same issue

Also seeing this same problem with turbo 2.3.2 and nextjs 15.0.3 and npm 10.8.2 Have tried everything @GavGrayston mentioned without success.

Build works fine without turborepo (e.g. remove workspaces in root package.json and “fixed” links to dependencies using “file:<path>” instead of “*”).

Maybe add a comment on https://github.com/vercel/next.js/issues/71873 which has the same error although it uses pnpm it could indicate that there is something up with module resolution when running next build/