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.
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
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..
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",
}
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.
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
- take the schema and generate types for it
npx prisma generate
- make sure the server is built if you use a Prisma type
npm run build
- update the production database based on your schema
npx prisma migrate deploy
- 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"]
© 2022 - 2024 — McMap. All rights reserved.
prisma generate
command, done automatically or manually, would require the Prisma CLI to be installed. – Gelsemium