How to run `prisma generate` in production?

Viewed 6566

I'm confused about how Prisma code generation in production works. The Prisma CLI is supposed to be installed in devDependencies, yet the npx prisma generate command needs to be available in production, since the generated code is necessary for the application. How can I resolve this? I tried running npm i --production and npx prisma generate, which led to the expected problem of npx trying to auto-install prisma and getting Prisma 1 instead of Prisma 2 and then expecting a prisma.yml file which doesn't exist.

2 Answers

Prisma has various guides for installing on different environments. For example, this one talks about installing on vercel.

https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-vercel

Postinstall hook The package.json uses the postinstall hook script to run prisma generate. Typically this would go in the build step. Because Vercel caches node_modules after the dependencies are installed, the functions won't have access to the generated Prisma Client.

Generating the Prisma Client in postinstall ensures that the generated Prisma Client in node_modules/@prisma/client is available to the functions.

Related