Error with deploying my project

My project is separated with two sub dirs: frontend and backend

The package.json for backend is in the root dir and there is a NextJS for the frontend. When running npm run build locally with this:

    "build": "npm install && npm install --prefix frontend && npm run build --prefix frontend"

There is no problems, however when building on vercel there is an error with files not installed:

but they are in package.json in the backend dir:

Hi, @cchryx! Welcome to the Vercel Community.

I think that you need to adjust your Vercel configuration to ensure it correctly installs dependencies for both your frontend and backend. Here’s how you can do that:

  1. Create a vercel.json file in the root of your project if you haven’t already.
  2. In the vercel.json file, specify the build command and the output directory. Here’s an example configuration:
{
  "version": 2,
  "builds": [
    {
      "src": "package.json",
      "use": "@vercel/node",
      "config": { "includeFiles": ["backend/**"] }
    },
    {
      "src": "frontend/package.json",
      "use": "@vercel/next"
    }
  ],
  "routes": [
    { "src": "/api/(.*)", "dest": "backend/api/$1" },
    { "src": "/(.*)", "dest": "frontend/$1" }
  ]
}

This configuration does the following:

  • It specifies two build steps: one for the backend and one for the frontend.
  • The backend build step uses @vercel/node and includes all files in the backend directory.
  • The frontend build step uses @vercel/next for your Next.js application.
  • The routes configuration directs API requests to the backend and all other requests to the frontend.
  1. Modify your root package.json to include a build script that installs dependencies for both frontend and backend:
{
  "scripts": {
    "build": "npm install && npm install --prefix frontend && npm run build --prefix frontend"
  }
}
  1. In your Vercel project settings, make sure the “Build Command” is set to npm run build.
  2. Set the “Output Directory” to frontend/.next in your Vercel project settings.

Could you give that a go?

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