Out of the box nextjs pnpm monorepo won't boot: Cannot find module 'next/dist/server/lib/start-server.js'

https://matestma.netlify.app/

The repro repo is available here: GitHub - makeagent/ma-next

This is a brand new Next 15.3 site using pnpm. I then moved it into a web folder to simulate a basic monorepo. The build succeeds, but loading the page fails with:

May 6, 09:29:33 AM: 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. (/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. (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)"]}

I’ve tried various different approaches as per the commit history of that repo, but the best outcome is that it builds, and then throws this error on launch…

1 Like

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.

Helpful to see that all other variables remaining the same the site can be deployed. It is just the monorepo that causes issues.

Did you refer to: Next.js on Netlify - OpenNext?

I did indeed. try the hoist · makeagent/ma-next@6604307 · GitHub

I also tried the npmrc method. No luck on both accounts.

The netlify.toml needs to be in the root of your workspace, not in the repo root.

After hours of digging I found what the SOLUTION is:
If you have a pnpm monorepo and besides the main pnpm-lock file in the root, you have a seperate pnpm-lock in one of your apps(subrepos), just delete that and problem will be solved!

Seeing the same issue in my project - builds successfully, but fails at runtime:

Error identical to op.

Edit: builds and runs on my machine using the same configuration, so like others are saying, this is probably some compatibility issue.

using turborepo w. pnpm in a monorepo setup

Hi, @lassejaco2. I cannot find that request id (which appears to be `01K1BD204YM0XJR3VMΚΗΚ01M64`) anywhere in our access logs. What site is this about?

Same issue using Turbo w/ npm monorepo.
Structure :

project/

├─ apps/

│  ├─ web/ (NextJS)

│  │  ├─ ... stuff

│  ├─ mobile/

│  │  ├─ ... other stuff

│- ... stuff

└─ netlify.toml

netlify.toml content :

[build]
  base = "apps/web"
  command = "npm run build"
  publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

If that’s the structure of your monorepo, the netlify.toml should most likely be in your web folder and the configuration should be as follows:

[build]
  command = "npm run build"
  publish = "apps/web/.next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

In the UI, remove all other values and set package-directory as apps/web.

I’ve updated the netlify.toml as you recommended, but now I’m seeing this error in the preview: (Invocation Failed).

netlify.toml:

[build]
  command = "npm run build"
  publish = "apps/web/.next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

Do you have a reproduction to share?

How can I share you a reproduction ?

Share a zip of your project via any cloud service.

@hrishikesh

Netlify fails to build the monorepo. After lots of trial and error netlify still doesn’t managed to build and deploy nextjs monorepo successfully.

I’ve monorepo of nextjs (turborepo) application but it doesn’t work.

Documentation seems really confusing. netlify.toml seems also confusing like where to put and what value we should put.

Like we should put it inside ./ or ./apps/my-nextjs-app

I advice netlify team to just create a project via turborepo, nextjs and try to deploy your own project on your own infrastructure, You will not able to get it running without atleast 2~3 days trial and error and tweaking.

First managing to make successful build is hard in monorepo of turborepo and nextjs.

But if you managed to build somehow then even after successful build it doesn’t work.

In my local system I just run command like yarn build:app-name from base dir or just run cd apps/app-name and it just works as expected.


Then other thing comes like open-next which seems more cofusing (I blame vercel for it).

As per docs netlify supports NextJS via open-next out of the box via just plug-n-play by just adding @netlify/plugin-nextjs somewhere is repo (Yeah! somewhere because I don’t exactly know to put it on ./package.json or ./apps/my-app/package.json


After lots of trial and error I’ve managed to build and publish successfully but still getting error “Invocation Failed”

This function has crashed
An unhandled error in the function code triggered the following message:

Error - Cannot find module 'next/dist/server/lib/start-server.js' Require stack: - /var/task/.netlify/dist/run/next.cjs

Stack trace
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:1401:15)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1057:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1062:22)
    at Function._load (node:internal/modules/cjs/loader:1211:37)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Module.require (node:internal/modules/cjs/loader:1487:12)
    at require (node:internal/modules/helpers:135:16)
    at Object.<anonymous> (/var/task/.netlify/dist/run/next.cjs:499:30)
    at Module._compile (node:internal/modules/cjs/loader:1730:14)
Connection details
Netlify internal ID: 01K5J6YXC31NR2JNGPPV8BARDS

Next steps
Site visitors: Contact the site owner or try again.
Site owners: Review the function logs for more info.

I’m sharing the my settings as much as I can. After sharing it I know netlify team will definately say “Oh! your this cofig should look like this not like that…” and I know it could be wrong since I’ve tried to official docs, generative AIs, forum answers, but none of them worked.

Only worked is my own trial-n-error and back-n-forth tweaking.

But after managing to build it somehow it’s failing in production getting above shared error. btw, I cannot share the whole codebase but I’ll share the all codes which you need like package.json, netlify.toml, Netlify website build settings.

I know netlify support team are really great and amazing since they atleast read all the forum comments/post and they reply on it even if it’s already covered on docs.

Let’s finish the turborepo+nextjs+open-next+netlify+@netlify/plugin-nextjs here. So in future others can take it as a ref.

Thank you!

./ = my base turborepo

./package.json

{
  "private": true,
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev",
    "dev:ads": "turbo run dev  --filter=ads",
    "build:ads": "turbo run build  --filter=ads",
    "lint": "turbo run lint",
    "format": "prettier --write \"**/*.{js,jsx,ts,tsx,md}\""
  },
  "devDependencies": {
    "eslint": "^8.48.0",
    "prettier": "^3.0.3",
    "tsconfig": "*",
    "turbo": "^2.4.0",
    "@netlify/plugin-nextjs": "^5.13.2"
  },
  "name": "my-masterrepo",
  "packageManager": "yarn@1.22.19",
  "workspaces": [
    "apps/*",
    "packages/*",
    "extensions/*"
  ],
  "dependencies": {
    "@types/xml2js": "^0.4.14",
    "xml2js": "^0.6.2"
  }
}

./apps/ads/netlify.toml

[build]
  command = "yarn build:ads"
  publish = "apps/ads/.next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

./apps/ads/package.json

{
  "name": "ads",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "next dev -p 3014",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@serwist/next": "^9.0.9",
    "cookies-next": "^6.1.0",
    "cors": "^2.8.5",
    "my-tailwind-config": "*",
    "my-ui": "*",
    "my-utils": "*",
    "mongoose": "^7.1.1",
    "next": "14.2.0",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@netlify/plugin-nextjs": "^5.13.2",
    "autoprefixer": "^10.4.13",
    "eslint-config-next": "12.1.5",
    "postcss": "^8",
    "tailwindcss": "^3.4.13"
  }
}

./apps/ads/next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  transpilePackages: ["my-ui", "my-utils", "my-tailwind-config"],
};

module.exports = nextConfig;

./turbo.json

{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": [
    "**/.env.*local"
  ],
  "tasks": {
    "build": {
      "dependsOn": [
        "^build"
      ],
      "outputs": [
        ".next/**",
        "!.next/cache/**"
      ]
    },
    "lint": {},
    "dev": {
      "cache": false,
      "persistent": true
    }
  }
}

If need any additional info please let me know.

I use below commands

Assuming I’m in ./

If I want to build all repo in one command: yarn build
If I want to build specific project: cd apps/my-app && yarn build
Run specific project: yarn dev:my-app (which you can check in ./package.json)


Btw, someone, somewhere suggested If I’ll make it standalone and let make node_modules in apps/my-app dir then it will start working (I’ve not tried it, will try this as well)


Below are my netlify dashboard settings Project configuration > Build & Deploys > Build Settings

Build settings

Runtime: Next.js
Base directory: /
Package directory: apps/ads
Build command: yarn build:ads
Publish directory: .next
Functions directory: netlify/functions
Build status: Active

And this setting is atleast allowing to build & deploy in netlify successfully (but failing to load website: “Invocation Failed”)

original post: This function has crashed An unhandled error in the function code triggered the following message: Error - Cannot find module ‘next/dist/server/lib/start-server.js’ Require stack: - /var/task/.netlify/dist/run/next.cjs

@knowivate I appreciate the details, but there was no need to post this twice. This made me read the same thing twice, which was some time I could have spent in actually working on the issue.

For what it’s worth, I’ve deployed a Next.js app with Turbo multiple times in the past and it worked, but it won’t hurt to try again just to be sure nothing is broken. I’ll reply in your original thread and will lock this thread as the original one seems irrelevant at this point.

1 Like