Fix 'prisma/client did not initialize yet' on Vercel

By

Fix the @prisma/client did not initialize yet error when deploying to Vercel by adding a postinstall script that runs prisma generate in your package.json.

~~~

The fix for the @prisma/client did not initialize yet error on Vercel is adding a postinstall script to your package.json that runs prisma generate. Here’s the full story.

I built an app with Next.js and Prisma, and when I tried to deploy it on Vercel I got this deployment error:

Vercel deployment error screen showing @prisma/client did not initialize yet error with stack trace

Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

The database was already initialized from my local dev install, and I just had to use it. Everything worked on my machine. Only the Vercel build failed.

Why does this happen on Vercel?

@prisma/client is not a normal package. The actual client code, typed from your schema, is generated when prisma generate runs. Installing the package triggers that generation through its own install hook.

The problem: Vercel caches your dependencies between builds to speed things up. When a later deployment reuses the cache, the packages aren’t reinstalled, the hook never fires, and the generated client is missing or stale.

That’s why the error message tells you to run prisma generate. It needs to run on every build, and the caching skips it.

The fix

What did I do to solve?

I installed prisma as a dev dependency:

npm i -D prisma

This matters because the prisma CLI is what runs the generation, and it must be available during the build on Vercel.

Then I added

"postinstall": "prisma generate"

to the scripts in package.json:

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "postinstall": "prisma generate"
  },
  "dependencies": {
    //...
  },
  "devDependencies": {
    //...
    "prisma": "^2.24.1",

  }
}

That solved the problem. The postinstall script runs after every install on Vercel, cached or not, so the client is always regenerated from the current schema.

You can hit the same error locally

This error is not exclusive to Vercel. You’ll see it after cloning a project and running the app before generating the client, or when the generated client doesn’t match a schema you just changed.

The fix in that case is one command:

npx prisma generate

With the postinstall script in place, a plain npm install takes care of it too.

Tagged: Database · All topics
~~~

Related posts about database: