@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again
Asked Answered
D

13

33

I am using prisma, postgres, docker, kubernets.

npx prisma migrate dev working.

and npx prisma generate produce below output:

✔ Generated Prisma Client (2.23.0) to ./node_modules/@prisma/client in 68ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

but when I tried to use in my route file produce the error:

new-route.ts

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

my docker file:

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .

CMD ["npm", "start"]
Deerstalker answered 29/5, 2021 at 1:3 Comment(2)
I think if you copy COPY prisma ./prisma/ that should work. But I'm a few steps behind you as I can't get the npx prisma migrate dev to work with the error message: Error: P1001: Can't reach database server at auth-postgres-srv.default.svc.cluster.local:5432Henbit
Just wanted to add my 2 cents to this. Recently spent half a day trying to figure out why my schema changes weren't being reflected when calling Prisma service. Turns out I had to restart the node environment or as I like to do it just restart the whole IDE and voila the changes start reflecting.Diversification
W
10

I usually don't use docker for this while developing, but I have this issue every time I change something in my schema.prisma and have to use npx prisma generate. The solution for me is to restart the node application running npm start again. Maybe if you restart your containers it might work.

if you are inside kubernets pod then access the pod using terminal then give generate command

kubectl exec -it pod_name sh
npx prisma generate
Witchy answered 29/5, 2021 at 1:59 Comment(1)
The solution given by Rafiq to run npx prisma generate in the pod is likely a bad one - next time the pod is restarted it will fail again. Just copy the prisma dir when building your imagePilloff
S
16

I know that this has been marked as solved, but I just wanted to share my setup for anyone interested.

Dockerfile

# Build image
FROM node:16.13-alpine as builder
WORKDIR /app

# Not sure if you will need this
# RUN apk add --update openssl

COPY package*.json ./
RUN npm ci --quiet

COPY ./prisma prisma
COPY ./src src
RUN npm run build

# Production image

FROM node:16.13-alpine
WORKDIR /app
ENV NODE_ENV production

COPY package*.json ./
RUN npm ci --only=production --quiet

COPY --chown=node:node --from=builder /app/prisma /app/prisma
COPY --chown=node:node --from=builder /app/src /app/src

USER node

EXPOSE 8080
CMD ["node", "src/index.js"]

package.json

{
  "name": "example",
  "description": "",
  "version": "0.1.0",
  "scripts": {
    "generate": "npx prisma generate",
    "deploy": "npx prisma migrate deploy",
    "dev": "npm run generate && nodemon --watch \"src/**\" --ext \"js,json\" --exec \"node src/index.js\"",
    "build": "npm run generate",
    "start": "npm run build && node build/index.js"
  },
  "prisma": {
    "schema": "prisma/schema.prisma"
  },
  "dependencies": {
    "@prisma/client": "^3.6.0"
  },
  "devDependencies": {
    "@tsconfig/node16": "^1.0.2",
    "@types/node": "^16.11.12",
    "nodemon": "^2.0.15",
    "prisma": "^3.6.0"
  }
}

I run this in Kubernetes. To make things smooth with database and migrations I run an initContainer that runs the prisma migrate deploy.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: EXAMPLE
spec:
  replicas: 1
  selector:
    matchLabels:
      app: EXAMPLE
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: EXAMPLE
    spec:
      containers:
        image: DOCKER_IMAGE
        imagePullPolicy: IfNotPresent
        name: SERVICE_NAME
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
      initContainers:
      - command:
        - npm
        - run
        - deploy
        image: DOCKER_IMAGE
        imagePullPolicy: IfNotPresent
        name: database-migrate-deploy

(This is a live service I just copied and stripped away anything non essential)

I hope this could be of use to someone

Selfstarter answered 16/12, 2021 at 9:50 Comment(5)
how do you run prisma migrations and seed for production? you just ran generate in DockerfileHizar
thanks for this, very useful. However, can you explain why you copy your node_modules from your build image? Doesn't that overwrite your lean production node_modules with all the dev dependencies and bloat your final image?Arnica
@JPLew You're absolutely correct, that statement should not be there. I'm updating my answer.Selfstarter
@markokraljevic As far as i know npx prisma generate only creates the ./prisma/generated directory. So it makes perfect sense to have that in the Dockerfile.Selfstarter
And I am facing this error , ``` Error: Unknown binaryTarget linux-arm64-openssl-undefined and no custom engine files were provided. I have added the binary target in schema.prisma but I couldn’t resolve the error. binaryTargets = ["native", "rhel-openssl-1.0.x"]```Vocalise
W
10

I usually don't use docker for this while developing, but I have this issue every time I change something in my schema.prisma and have to use npx prisma generate. The solution for me is to restart the node application running npm start again. Maybe if you restart your containers it might work.

if you are inside kubernets pod then access the pod using terminal then give generate command

kubectl exec -it pod_name sh
npx prisma generate
Witchy answered 29/5, 2021 at 1:59 Comment(1)
The solution given by Rafiq to run npx prisma generate in the pod is likely a bad one - next time the pod is restarted it will fail again. Just copy the prisma dir when building your imagePilloff
V
9

Here is another way to solve this.

Since the .prisma folder is needed by the prisma client as shown in the picture below or in the docs another way is to make sure it ships with your code. You can do this as follows.

Wrong way: ship the generated folder

You would think you can just include the generated files in the image by adding an exception rule for the .prisma folder to your .dockerignore (notice the exclamation point)

node_modules/
!nodes_modules/.prisma

But the query engine used by prisma is different for each operating system so you could run into troubles.

Correct way : generate the files with the image

Just add RUN npx prisma generate to your Dockerfile before the build command. This way the files are generated during the image creation and you don't have to run the prisma generate command on every container. The downside of this method is that your docker image will be larger. If this is an issue you can try with the other answers.

.prisma folder is needed to reference your generated code

V1 answered 5/10, 2022 at 14:57 Comment(4)
No need to generate if you copy prisma folder before running npm installPilloff
using "prisma generate" requires to have the development npm dependencies which is not good for production build, how do you handle this? @Pilloff - you want all to start from a plain base docker image and do not import anything from outside.Jarredjarrell
@IvailoBardarov commands run with npx do not get installed in the local folder. You can use a build stage if you want to build a docker image and are worried about including more than needed. I am happy to try to help you with a questions about npm, Docker and/or avoiding dev dependencies, feel free to ask a question and link it herePilloff
@Pilloff you are absolutely right, by using npx we can generate the client without installing the package. Thank you for pointing it!Jarredjarrell
R
8

You forgot to copy the prisma directory, since generating the Prisma Client requires the schema.prisma file. You should copy the whole prisma directory in case you also need the migrations.

Your final Dockerfile should contain the following:

WORKDIR /app
COPY package*.json .
COPY prisma ./prisma/ 
RUN npm install --only=prod
Remonstrant answered 3/7, 2022 at 12:28 Comment(2)
Thank you! This is the correct answer, simply copy this folder before running install and it will workPilloff
I was confused why I was receiving the error even though I was copying the prisma folder in my Dockerfile. But as you pointed out, it needs to be copied before running npm installOutlay
V
4

For me, I just changed my start under scripts in my package.json so that my app would generate prisma types before running:

"start": "npx prisma generate && nodemon server.ts"

In production:

"start": "npx prisma db push && npx prisma generate && node ./build/server.js"

This worked for me – I just wanted to drop this here for anyone who runs into the same issue.

Verdun answered 17/1, 2023 at 3:52 Comment(0)
M
1

This error happens because docker doesn't install devDependencies, which means that it doesn't pick up the Prisma CLI.

To remedy this you can add the prisma CLI package to your dependencies instead of the devDependencies. (Making sure to run npm install afterward to update the package-lock.json)

Motel answered 11/10, 2022 at 20:18 Comment(0)
W
1

I was having the same issue using prisma, mongodb and nextjs while trying to deploy to vercel. I added this to my package json;

...
"vercel-build": "npx prisma generate && next build"
...

It worked on me.

Wageworker answered 18/9, 2023 at 12:18 Comment(0)
J
0

If you use docker compose try this:

  1. add this to your Dockerfile

    COPY prisma ./prisma/

  2. add the prisma directory to your volume in docker-compose.yml file

    volumes:
      - /app/prisma
    

in my case docker-compose.yml file will look like this

version: "3"

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: web
    restart: always
    volumes:
      - ./:/app
      - /app/node_modules
      - /app/.next
      - /app/prisma
    ports:
      - 3000:3000
Jethro answered 18/9, 2023 at 10:30 Comment(0)
B
0

I changed the import adress from: "@prisma/client" or "@prisma/client/edge" to: "../../prisma/generated/client/edge"

I address the directory where generated folder is created in prisma folder.

Bigg answered 19/11, 2023 at 17:8 Comment(0)
G
0

I was using next js with Prisma and when deploy it to Vercel it showed me this error, I just added "postinstall":"npx prisma generate" in the scripts of package.json file

Guaranty answered 13/12, 2023 at 16:13 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Otorhinolaryngology
W
0

Based on this issue in Prisma repo, you should add output to schema.prisma:

generator client {
  provider = "prisma-client-js"
  output   = "node_modules/.prisma/client"
}

‍‍

Willable answered 27/1 at 14:32 Comment(0)
A
0

If you are using node js and then please take care of spelling mistakes in schema.prisma file. In my case I have write modal instead of model.

    model matches {
  id          Int    @id @default(autoincrement())
  name        String    
  matchType   String    
  matchStatus MatchStatus    
  matchResult String   
  isDeleted   Boolean   @default(false)
  createdAt   DateTime  @default(now())
  modifiedAt  DateTime  @default(now())
  createdBy   Int
  modifiedBy  Int
}

and if this is not working then please update the package.json file below way.

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npx prisma generate && nodemon app.js"
  },

Thanks!

Adrien answered 9/4 at 9:18 Comment(0)
H
0

I found the solution to this - in the package.json for the root directory, under scripts , add "postinstall": "prisma generate --schema=<Enter the location to your prisma.schema file>"

Huneycutt answered 13/4 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.