Prisma Client isn't generated on deploying to Vercel
See original GitHub issueBug 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:
- Change is made to the Github
- Vercel sees the changes
- Checks if anything has changed in
package-lock
- 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:
- Created 3 years ago
- Reactions:3
- Comments:15 (6 by maintainers)
Top GitHub Comments
I was able to reproduce this problem and solve it by adding the
postinstall: prisma generate
topackage.json
.This was confirmed by the Vercel team who clarified that:
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.Closing this issue as it was due to misconfiguration.
If you encounter this error, ensure that you have
prisma generate
in thepostinstall
hook. This is because thepostinstall
hook runs after thenode_modules
is loaded from cache on Vercel.