nestjsprisma

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


I am using pnpm nestjs@11.0.1 and Prisma 6.19.0 for a backend application. When I add the Prisma service, I get the error

 Nest] 46276  - 17/11/2025, 12:45:49   ERROR [ExceptionHandler] Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

I have run pnpx prisma generate. This returns Generated Prisma Client (6.19.0) to ./generated/prisma

Based on the NestJs site, I have updated tsconfig.build.json to have the entry:

"include": ["src", "generated"],

In schema.prisma I have:

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

In the prisma.service.ts I have:

import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient     implements OnModuleInit {
  async onModuleInit() {
  await this.$connect();
 }
}

If it would help, I can add the project to GitHub.

I am unclear how to correct the error


Solution

  • Recently, there were ground-breaking changes to how the Prisma ORM will be integrated.

    Prisma now uses adapters like this:

    
    import { PrismaClient } from '../../../generated/prisma/client.js';
    
    import { PrismaPg } from '@prisma/adapter-pg';
    
    const adapter = new PrismaPg({
      connectionString: process.env.DATABASE_URL,
    });
    
    const prismaClientSingleton = () => {
      return new PrismaClient({ adapter: adapter });
    };
    
    declare const globalThis: {
      prismaGlobal: ReturnType<typeof prismaClientSingleton>;
    } & typeof global;
    
    export const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();
    
    if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma;
    

    You can create this in a lib/db.ts or somewhere in your source folder.

    Simply import the prisma anywhere in your project like this:

    import { prisma } from './lib/db.js';
    

    And can then call the user (depends on what entity you have generated in your schema.prisma file) like this:

    async create(createUserDto: Prisma.UserCreateInput) {
        return prisma.user.create({
          data: createUserDto,
        });
      }