vercel: Serverless functions of Next.js pre-build deployments don't work when project is a monorepo
When deploying a pre-build Next.js app via the Vercel CLI from insight a pnpm monorepo, serverless functions fail with the following error stacktrace:
2022-10-27T12:17:54.580Z undefined ERROR Cannot find module 'next/dist/server/next-server.js'
Require stack:
- /var/task/___next_launcher.cjs
2022-10-27T12:17:54.580Z undefined ERROR Did you forget to add it to "dependencies" in `package.json`?
2022-10-27T12:17:54.820Z undefined ERROR Cannot find module 'next/dist/server/next-server.js'
Require stack:
- /var/task/___next_launcher.cjs
2022-10-27T12:17:54.820Z undefined ERROR Did you forget to add it to "dependencies" in `package.json`?
RequestId: d158d50e-ba20-4b31-9511-fb9a884794e4 Error: Runtime exited with error: exit status 1
Runtime.ExitError
Deploying the same apps without a workspace/monorepo setup works without any issues.
Steps to reproduce can be found in this example repo.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 14
- Comments: 30 (1 by maintainers)
This issue is resolved for me after talking with @TooTallNate
Fix for me was the following:
30.2.2apps/appname/client)vercel link --repoapps/appname/client)I had the same issue and the only way to overcome it was by installing using NPM. The steps I followed:
deploy-package.jsonthat I substituted all PNPMworkspace:^byfile:... For example, instead of"@packages/next-revalidate": "workspace:^"I use"@packages/next-revalidate": "file:../../../packages/next-revalidate".We need to keepworkspace:^because Turbo needs to create its graph.node_modulesfrom the package.package.jsonwithdeploy-package.json(this way NPM can install other monorepo packages).npm i.The problem with PNPM is its symlinks that for some reason are not bundled/copied when we run
vercel build.Facing a similar issue here. I’ve created a reproduction repo here: davewasmer/throwaway-sandbox.
If I create a Vercel app and connect it to this repo using the Git integration, the deploys work just fine. But if I prebuild the app and attempt to deploy the prebuilt version, the serverless function errors out with:
If I try to deploy the app from my command line using
vercel deploy, but without prebuilding, it errors out with:The error is repeated:( https://web-one-alpha-67.vercel.app/news
NextJs 14.1 vercel 33.2.0 turbo 1.12.2 pnpm
@trappar
Your issue in that reproduction steps is you’re not running the build command from the root of the repository.
https://vercel.com/docs/concepts/monorepos#using-monorepos-with-vercel-cli
You need to set the
Root Directorytoapp, and run the build command from the repo root, not the project root.@dlehmhus, thanks for your reply.
In the meantime I’ve got our setup working with the latest versions of vercel, nx and nextjs:
On CI I did have an issue with Nx cache. It had cached my local build with a binary dependency for MacOS, which of course didn’t work on our CI or in vercel. So our build skips the Nx cache in CI:
I’m not using NX. In my actual monorepo where I’m experiencing the issue I’m using Turborepo. When I said I reproduced it again I meant by deploying the reproduction repo I linked, which doesn’t use NX or Turborepo and still has the same issue.