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)

Most upvoted comments

This issue is resolved for me after talking with @TooTallNate

Fix for me was the following:

  1. Update vercel to 30.2.2
  2. Set each client application rootDirectory to the real root directory (apps/appname/client)
  3. Link repo in root via vercel link --repo
  4. Build as normal (running build commands from the app folder (apps/appname/client)

I had the same issue and the only way to overcome it was by installing using NPM. The steps I followed:

  1. I install and build all my monorepo with Turbo and PNPM as usual on the root.
  2. cd to package folder.
  3. I’ve created a deploy-package.json that I substituted all PNPM workspace:^ by file:... For example, instead of "@packages/next-revalidate": "workspace:^" I use "@packages/next-revalidate": "file:../../../packages/next-revalidate". We need to keep workspace:^ because Turbo needs to create its graph.
  4. Removed the node_modules from the package.
  5. Replaced package.json with deploy-package.json (this way NPM can install other monorepo packages).
  6. Run npm i.
  7. Then build and deploy using Vercel.

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:

Cannot find module 'next/dist/server/next-server.js'
Require stack:
- /var/task/___next_launcher.cjs
Did you forget to add it to "dependencies" in `package.json`?
RequestId: a0a95830-8aba-45e5-b7c5-76b72bd119af Error: Runtime exited with error: exit status 1
Runtime.ExitError

If I try to deploy the app from my command line using vercel deploy, but without prebuilding, it errors out with:

$ pnpm vercel deploy --cwd apps/sandbox --prod
[...]
Error: No Next.js version could be detected in your project. Make sure `"next"` is installed in "dependencies" or "devDependencies"

const path = require(“path”);

/** @type {import(‘next’).NextConfig} */ const nextConfig = { reactStrictMode: true, // transpilePackages: [“ui”], output: “standalone”, experimental: { outputFileTracingRoot: path.join(__dirname, “…/…/”), }, };

adding this to next config worked don’t forget to bring in path

The error is repeated:( https://web-one-alpha-67.vercel.app/news

NextJs 14.1 vercel 33.2.0 turbo 1.12.2 pnpm

image

@trappar

I’m having this same issue. I also created a minimal reproduction repo for the issue with instructions on how to reproduce. > See the repo here: https://github.com/trappar/next-vercel-ssr-bug

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

Vercel CLI should always be invoked from the monorepo root, not the subdirectory.

You need to set the Root Directory to app, and run the build command from the repo root, not the project root.

Screenshot 2023-05-16 at 17 50 58

@dlehmhus, thanks for your reply.

In the meantime I’ve got our setup working with the latest versions of vercel, nx and nextjs:

Dependency Version
vercel 28.18.1
nx 15.8.7
next 13.2.4

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:

npx nx build my-app --prod --skip-nx-cache

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.