Custom output path with prisma-client (esm) in NestJS
Hi i am trying to use prisma-client with esm (rust engine free version) of prisma v6.16.0
// schema.prisma
But when i run my NestJS application i get this error:
what am i doing wrong. any guidance/help is appreciated. thanks.
13 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
The error
You need to explicitly set the
If your project is actually using CommonJS, set
If you still encounter issues, double-check your NestJS build and runtime configuration to ensure it matches the module format of the generated Prisma Client. Also, make sure you are importing the client using the correct path and syntax for ESM. For more details, see the Prisma generator documentation. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
ReferenceError: exports is not defined in ES module scope occurs because the generated Prisma Client is using CommonJS (exports) syntax, but your NestJS project (or at least the runtime) is running in ESM mode. This mismatch happens when the Prisma Client is not generated with the correct module format for your environment.
Solution:You need to explicitly set the
moduleFormat to "esm" in your generator block. With the new prisma-client generator, the default may not always match your runtime, so you must specify it:
After updating your schema.prisma, run npx prisma generate again to regenerate the client. This should produce ESM-compatible code that uses import/export instead of CommonJS exportsReferenceError: exports is not defined in ES module scope for provider = "prisma-client".
If you need CommonJS:If your project is actually using CommonJS, set
moduleFormat = "cjs" instead.
Note:If you still encounter issues, double-check your NestJS build and runtime configuration to ensure it matches the module format of the generated Prisma Client. Also, make sure you are importing the client using the correct path and syntax for ESM. For more details, see the Prisma generator documentation. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
@Prisma AI Help this is my tsconfig.json
We have a working example with Nuxt here:
https://github.com/prisma/prisma-examples/tree/latest/generator-prisma-client/nuxt3-starter-nodejs
Can you check if it helps?
GitHub
prisma-examples/generator-prisma-client/nuxt3-starter-nodejs at lat...
🚀 Ready-to-run Prisma example projects. Contribute to prisma/prisma-examples development by creating an account on GitHub.
I've also been having the same issues with NestJS. Never used or heard of Nuxt. Is it related to NestJS?
@Nurul (Prisma) any help for NestJS?
No Nuxt is not related to NestJS. Nuxt is a Vue based alternative to NextJS.
Anyways, I've set this up myself with a NestJS project just recently.
After generating the output, you need to change your imports, so you are no longer importing from
@prisma/client, an additionally you need to setup an adapter.
Have you done so?
I have a postgres implementation using the postgres adapter, right here:
Does this solve your problem?import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from './generated/prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
constructor(private configService: ConfigService) {
const adapter = new PrismaPg({
connectionString: configService.get<string>('DATABASE_URL'),
});
super({ adapter });
}
async onModuleInit() {
await this.$connect();
}
}
You're duplicating it.
Sorry, duplicating what? I'm not sure I follow 😄
Use the generated client output (from prisma generate) instead of importing PrismaClient directly from @prismal client.
Provide an adapter (PrismaPg, PrismaMysql, etc.) when constructing PrismaClient.
Thats exactly what I'm doing, but I recommended the same thing to OP. Did you mean to respond to OP?
Please just think of this as me talking to myself.d( ̄  ̄)
:aVerified:
@Gregersen thanks, I thought I had tried this but I might have missed something, will look into that solution... although I saw you posting in another thread I'm in over on the NestJS Discord after you sent me this solution... so is it not working?

@Eric Yeah so as it turns out this works in development, but in production I have been unable to initialize my app, after switching. So I decided to roll it back, as I simply cant find a solution for it 😬
Spending a bit of time with it this morning, it may seem like it's working after all. I'll report back.
Sorry for being a bit preemptive on my answer. I am not sure if the reason it isn't working for me, is because of my deployment pipeline.
Like I said the development instance is running, so technically the prisma client is working rust free 😄
I'm coming back and changing my answer again. This works great. Both in development and production.
There's a few changes necessary to make it work, among other, the changes I listed above.
For anyone interested in making this upgrade in NestJS I made a brief article on the topic, to describe the steps:
https://mgregersen.dk/upgrading-prisma-to-rust-free-client-in-nestjs/
I'm happy to answer questions, if things don't work out, or if I forgot anything.
Martin Aarhus Gregersen
Martin Gregersen
Upgrading Prisma to Rust-free client in NestJS - Martin Gregersen
In this article I’ll quickly go over the steps I took to update Prisma to the new rust free implementation, that is now “GA” as of Prisma version 6.16.0., as indicated by the recent blog on the topic: https://www.prisma.io/blog/rust-free-prisma-orm-is-ready-for-production Updating and configuring Prisma First things first, first we need to...