Preview Error: this function has crashed (All deploy steps are success)

Hello Netlify Community and Support,

I am deploying a Next.js application which is part of a Yarn monorepo to Netlify. The build and deploy steps are reported as successful in the Netlify dashboard, but when I try to open the deployed site URL, I receive an error.

Upon checking the function logs, I see the following error repeatedly:

May 5, 07:54:21 PM: ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Cannot find module 'next/dist/server/lib/start-server.js'\nRequire stack:\n- /var/task/.netlify/dist/run/next.cjs", ...}

(This is the exact error message from my logs, timestamps may vary).

This indicates that a required Next.js server module is not being found when the serverless function is invoked at runtime.

My site URL: https://vedvaani.netlify.app/

Next.js project all deploy steps success, but when open preview site, it gives error.

Function log has error:
May 5, 09:46:43 PM: ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Cannot find module 'next/dist/server/lib/start-server.js'\nRequire stack:\n- /var/task/.netlify/dist/run/next.cjs","reason":{"errorType":"Error","errorMessage":"Cannot find module 'next/dist/server/lib/start-server.js'\nRequire stack:\n- /var/task/.netlify/dist/run/next.cjs","code":"MODULE_NOT_FOUND","requireStack":["/var/task/.netlify/dist/run/next.cjs"],"stack":["Error: Cannot find module 'next/dist/server/lib/start-server.js'","Require stack:","- /var/task/.netlify/dist/run/next.cjs"," at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)"," at Function._load (node:internal/modules/cjs/loader:1055:27)"," at TracingChannel.traceSync (node:diagnostics_channel:322:14)"," at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)"," at Module.require (node:internal/modules/cjs/loader:1311:12)"," at require (node:internal/modules/helpers:136:16)"," at Object.<anonymous> (/var/task/.netlify/dist/run/next.cjs:498:30)"," at Module._compile (node:internal/modules/cjs/loader:1554:14)"," at Object..js (node:internal/modules/cjs/loader:1706:10)"," at Module.load (node:internal/modules/cjs/loader:1289:32)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Cannot find module 'next/dist/server/lib/start-server.js'","Require stack:","- /var/task/.netlify/dist/run/next.cjs"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:518:28)"," at emitUnhandledRejection (node:internal/process/promises:252:13)"," at throwUnhandledRejectionsMode (node:internal/process/promises:388:19)"," at processPromiseRejections (node:internal/process/promises:475:17)"," at process.processTicksAndRejections (node:internal/process/task_queues:106:32)"]}

Build Settings:
Runtime: Next.js
Base directory: /
Package directory: apps/web-app
Build command: yarn workspace web-app build
Publish directory: apps/web-app/.next
Functions directory: netlify/functions
Build status: Active

Error screenshot:

Deploy shows success:

Function logs:

So, not just me. I think this is a straight compatibility issue…

After trying a LOT of options, I gave up and created a new repo and deployed the subtree of my main repo to it using git subtree, and that worked. Problem solved.

Thanks Daniel for sharing, is git subtree a temporary workaround?

It would be really helpful if you can share in detail or steps to solve it.

If anyone have a more simple solution, kindly share.

Thanks

@jaydeep-vv, do you have a minimal reproduction to share?

Hi everyone,

I want to pick up on this issue because we’re running into the exact same problem.

We have a Yarn monorepo with multiple workspaces, including several Next.js applications that we’re deploying to Netlify. Everything worked fine up until Next.js v12, but after upgrading all apps to Next.js v13, we’re running into the issue described in the original post:

  • Netlify build & deploy process reports success

  • But when opening the deployed site, we get:

    Error: Cannot find module 'next/dist/server/lib/start-server.js'
    
    

Current Build Settings (The Next.js project and netlify.toml are located in packages/projectX/frontend/):

  • Runtime: Next.js

  • Base directory: packages/projectX/frontend/

  • Package directory: (not set)

  • Build command: yarn run build(next build)

  • Publish directory: packages/projectX/frontend/.next

  • Functions directory: packages/projectX/frontend/netlify/functions

  • Build status: Active

The issue seems to stem from Next.js being hoisted to the root node_modules by Yarn. With Netlify Next.js Runtime v4 this wasn’t an issue, but with Runtime v5 it appears to break. Disabling hoisting is unfortunately not an option for us (it causes a cascade of other problems in our setup), even though it might be the cleanest long-term solution.

Is there a simple solution or recommended workaround for monorepos where Next.js is hoisted, so that deployments on Netlify work correctly with Runtime v5?

Thanks in advance for any guidance!

Package directory should be packages/projectX/frontend/ and base directory should be unset.