Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]
like image 424
Lord Avatar asked Aug 30 '25 16:08

Lord


2 Answers

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

like image 71
TenorDev Avatar answered Sep 02 '25 13:09

TenorDev


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
like image 22
Rômulo Bourget Novas Avatar answered Sep 02 '25 14:09

Rômulo Bourget Novas