How to deploy a pre-built .next folder to vercel?

Hi,

I have a monorepo setup:

repo 
  - package.json
  - BUILD.bazel
  - source/
    - frontend/
      - BUILD.bazel
      - next.config.js
      - app/
        - page.tsx
        - layout.tsx
        - BUILD.bazel

I manage to build this using bazel and run it locally without issues (e.g. bazel run source/frontend:next).
I can also build the project fine: bazel build source/frontend:next builds fine and creates a .next folder under bazel-bin/source/frontend/.next. This is all expected and working fine.

The problem is when I try to deploy this to vercel. I want to build locally and deploy the prebuilt artifacts, so my plan is to do
vercel build --prod && vercel deploy --prebuilt --prod

However, I am having trouble deploying the right files to vercel. My project.json file looks like this:

{
  "projectId": "...",
  "orgId": "...",
  "settings": {
    "createdAt": 1728198914318,
    "framework": "nextjs",
    "devCommand": null,
    "installCommand": "",
    "buildCommand": "bazel build //source/frontend:next",
    "outputDirectory": "bazel-bin/source/frontend/.next",
    "rootDirectory": null,
    "directoryListing": false,
    "nodeVersion": "20.x"
  }
}

The buildCommand executes and is successful, but the CLI then fails with Cannot find module 'next/dist/server/next-server.js'. I tried adding this next folder in many places (under .vercel/builders/node_modules, as a first level dir under the root, inside source/frontend, etc. - but to no avail). If I change the rootDirectory to be source/frontend, then I won’t have access to the build output, since bazel puts it into bazel-bin, two layers above the source/frontend dir - and from the docs, it looks like you cannot access anything upstream of the root dir, not even using .. to navigate back.

If I change framework choice to other, then vercel build command finishes successfully and the project is deployed to vercel, vercel does not run it properly (it raises a 404 Error - presumably because the files are not in the right format).

Ok so what is the solution here? How does one deploy a pre-build .next folder to vercel using the CLI ?

Hi,

would be great if I could get some help on this one :slight_smile:

Gentle ping :slight_smile:

Any link to any sort of documentation or example would be greatly appreciated!

Hey @ant0198. This might be related to specific project configuration details. Are you able to share a minimal reproducible example so we can work together from the same code to solve this?

Hey @amyegan ,

Thank you for your answer! Let me set one up, and I can share it with you.

In the meanwhile I am close to getting it to work, so perhaps it’s a simple fix now for someone more experienced.

The site now builds and vercel CLI is mostly happy (I see the Traced Next.js server files in: 22.833ms line).

But then it fails with Error: ENOENT: no such file or directory, lstat '/Users/user/Documents/repo/src/frontend/.next/routes-manifest.json' .

The problem of course is that it’s looking for the .next directory inside the /src dir, instead of using bazel-bin/src/frontend/.next .

If I manually copy the folder from bazel-bin inside src , then it works great and I can deploy the site.
So it’s really a question of getting vercel cli to use the bazel-bin path in the very last step. I have played around the configs but I cannot seem to find the right incantation.

Would welcome any suggestions - in the meanwhile I will set up a small repo.
Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.