How to keep the Prisma client up to date when deploying Next.js to Vercel

When Vercel deploys your Next.js, it might re-use cache from previous builds in order to speed up the process. If you frequently update your Prisma schema during development your build might fail due to an out of date Prisma client.

The Prisma client is generated based on your prisma.schema file. When you update your prisma.schema file you usually run the following command to update the client's .js files and your database at the same time.

npx prisma migrate deploy

When your project is deployed to Vercel, this command is not ran as part of the build process. Which can result in out of date Prisma .js files. Always make sure that the @prisma/client is up to date when deploying your Next.js project. One way to keep it up to date is to re-generate it before every build. Add the following script to your package.json scripts section

"prebuild": "prisma generate"

It will force the @prisma/client to be re-generated before your Next.js project is compiled. Doing this will solve any cache-related issues when deploying a Next.js project on Vercel using Prisma.

Example

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "prebuild": "prisma generate"
  },

You could also use prisma migrate deploy but doing so will also run migrations to your database. I personally keep my database updates separated from my CI/CD pipeline.