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
- Run
npm i --force(to avoid react@19 peer deps errors) - Run
npm run build - 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)
@GavGrayston, @ezeamin et. al… I found a fix for this for my use case - this may help someone else: Note that I’m using
npmfor my package manager, so your mileage may differ when usingpnpm,yarnetc.After performing
npm installfrom the root folder, my nextjs application (in theapps/web-serversub-folder for me) contained its ownnode_modulesfolder which only containedreactandreact-dompackages. The rootnode_modulesfolder also contained the same versions. For some reason,next buildseems to be confused by the dueling packages, so I simply deleted my app’snode_modulesfolder (i.e.apps/web-server/node_modulesin my case) and then re-ran the build. Voila! everything worked fine.This issue is happening also for Nx monorepo (npm workspace) When your
reactandreact-domat package.json mono repo root version are different fromreactandreact-domin the package.json in yourapp/projectThe temp fix is to delete
reactandreact-domin the node_modules in yourapp/projectbut this is not viable long runWhat 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-reactturbo task which I include as a dependency of thebuildtask. There’s obviously a corresponding entry in the app’s package.json scripts forclean-reactwhich does arm -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.
This worked !!
I am also being affected by prerendering errors on
/400and/500pages duringturbo run buildin 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.0Removing and regenerating
package-lock.jsonas mentioned by @inadeqtfuturs above has not fixed the errors in my case.edit: I ended up with a workaround fix of specifying
react19.0.0 andreact-dom19.0.0 in my top-levelpackage.jsonin addition to myapps/web/package.jsonfile.Prior to making this change, I only had
reactandreact-domin myapps/web/package.jsonand was ending up with a version mismatch caused by two copies of thereactandreact-dompackages within the monorepo:node_modules/reactandnode_modules/react-domcontained the 18.3 versionsapps/web/node_modules/reactandapps/web/node_modules/react-domcontained the 19.0 versionAfter adding
reactandreact-domto thepackage.jsonin my Turborepo root, I now have:node_modules/reactandnode_modules/react-domcontain the 19.0 versionapps/web/node_modulesis emptythanks @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/reactto 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.
I found that the root cause is an npm workspace depending on another, with both having
reactandreact-domin theirpackage.json.The solution is to make sure that
reactandreact-domare 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_modulefolders. i think because i made the transition from next 14 to 15, as well as from react 18 to 19, my rootpackage-lock.jsonstill 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_modulesas suggested by @fuzing, i ultimately just deleted my rootpackage-lock.jsonand 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
pnpmit could indicate that there is something up with module resolution when runningnext build/