question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma Client isn't generated on deploying to Vercel

See original GitHub issue

Bug description

As per the following discussion:

https://github.com/prisma/prisma/discussions/5140

How to reproduce

Deploying prisma to Vercel breaks

So I’ve started my project from the official example found here. This project works perfectly when deployed to Vercel directly because it forces Vercel to download, build and generate the prisma client.

Unfortunately, if you were to upload your project to Github and set up auto-deploys on commit, you will soon find a bunch of errors related to @prisma/client missing exports in your build outputs. For me these were missing types for my data and even native exports like empty and sql.

If you were to try to access your API you would most likely be able to see something like 2021-01-17T16:41:33.659Z undefined ERROR Error: You most likely forgot to initialize the Prisma Client. Please run prisma generate and try to run it again. in your function logs.

Why this happens

Vercel, like many other serverless hosts, uses a package-lock-based cache. What this means in practice is:

  1. Change is made to the Github
  2. Vercel sees the changes
  3. Checks if anything has changed in package-lock
  4. If it’s the same as the previous commit, it downloads a cache of node_modules

Unfortunately, node_modules is where the prisma client (which contains all of our types) is kept. And even more unfortunately, changes to our API which would be reflected in our types when running npm run generate does not make changes to package-lock.

In short this means that even though our app may have gone through significant changes, if package-lock isn’t deleted manually, our types will not be generated, which in turn will most likely break our API.

Expected behavior

A workaround should be possible to generate the PrismaClient in Vercel.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
2colorcommented, Jan 20, 2021

I was able to reproduce this problem and solve it by adding the postinstall: prisma generate to package.json.

This was confirmed by the Vercel team who clarified that:

Having prisma generate as the postinstall command is a way to ensure that is always run after the initial build step, regardless of whether it used the cached node_modules or not.

From my tests, it seems that the trick that we do when building on Vercel, doesn’t always actually work (CC: @timsuchanek).

@creativiii I’d suggest you add the postinstall script and see if that solves the problem for you.

6reactions
2colorcommented, Jan 25, 2021

Closing this issue as it was due to misconfiguration.

If you encounter this error, ensure that you have prisma generate in the postinstall hook. This is because the postinstall hook runs after the node_modules is loaded from cache on Vercel.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deploy a Prisma app to Vercel
1. Create and deploy the project with the Vercel deploy button · Vercel deploy button. Vercel prompts you to clone the repository: ·...
Read more >
How to solve the `prisma/client did not initialize yet` error on ...
I built an app with Next.js and Prisma, and when I tried to deploy it on Vercel I got this deployment error: Error:...
Read more >
How to deploy to Vercel with dynamically generated package ...
Prisma dynamically generates the Prisma client, but Vercel caches the old client and doesn't rebuild it unless I log in to Vercel and...
Read more >
I tried pushing my nextJs site to vercel and it failed with | Prisma
I tried pushing my nextJs site to vercel and it failed with some error saying: Error: @prisma/client did not initialize yet. Please run...
Read more >
Vercel CLI – Vercel Docs
... enabling you to retrieve logs, manage certificates, replicate your deployment environment locally, manage Domain Name System (DNS) records, and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found