How to run `prisma generate` in production?
Asked Answered
G

5

17

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.

Gelsemium answered 19/9, 2020 at 19:20 Comment(0)
S
3

There's no need to run the prisma generate command that is executed on installation of the @prisma/client.

EDIT: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/generating-prisma-client

Supergalaxy answered 20/9, 2020 at 2:15 Comment(1)
But running the prisma generate command, done automatically or manually, would require the Prisma CLI to be installed.Gelsemium
C
3

That's what I did in package.json file (It was a deploy a Next app on Versel) I just added generate command to the build script:

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

Not sure if it is a correct way, though..

Candler answered 26/7, 2023 at 19:10 Comment(0)
H
1

The Prisma package has a postinstall hook. From the docs:

The @prisma/client package defines its own postinstall hook that's being executed whenever the package is being installed. This hook invokes the prisma generate command which in turn generates the Prisma Client code into the default location node_modules/.prisma/client. Notice that this requires the prisma CLI to be available, either as local dependency or as a global installation. It is recommended to always install the prisma package as a development dependency, using npm install prisma --save-dev, to avoid versioning conflicts.

If you're deploying to Vercel you should know that Vercel caches the node_modules dependencies, so the Prisma docs have workarounds.

The tl;dr is if you're using Vercel for production add this to the scripts section of your package.json:

scripts: {
   // ... your other scripts
   "postinstall": "npx prisma generate",
}
Hooch answered 9/1 at 18:36 Comment(0)
R
0

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.

Refractory answered 7/6, 2021 at 5:31 Comment(0)
A
0

following Prisma documentation (check the url)

prisma generate is not affected by the environment, the only command that may be affected by the environment is prisma migrate

use this command npx prisma migrate deploy in the production environment

try to use commands

  1. take the schema and generate types for it
npx prisma generate 
  1. make sure the server is built if you use a Prisma type
npm run build
  1. update the production database based on your schema
npx prisma migrate deploy
  1. Check if everything is working well. The database has been built, and the environment file is loaded.
npx prisma migrate status 

if you are using GitHub action and docker, you can to build the image like this

FROM node:latest
WORKDIR /apps
COPY apps/users/package*.json ./

COPY apps/users ./

RUN npm install
RUN npm install --save-dev @types/node

RUN npx prisma generate
RUN npm run build
RUN npx prisma migrate deploy
RUN npx prisma migrate status

EXPOSE 3000:3000

CMD ["npm", "run", "start"]
Arrestment answered 13/6 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.