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
generator client {
provider = "prisma-client"
engineType = "client"
previewFeatures = ["relationJoins"]
output = "../generated/prisma"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client"
engineType = "client"
previewFeatures = ["relationJoins"]
output = "../generated/prisma"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
But when i run my NestJS application i get this error:
file:///home/alifarooq/Documents/Workspace/cannabis-automation-be/dist/generated/prisma/client.js:38
Object.defineProperty(exports, "__esModule", { value: true });
^

ReferenceError: exports is not defined in ES module scope
at file:///home/alifarooq/Documents/Workspace/cannabis-automation-be/dist/generated/prisma/client.js:38:23
at ModuleJobSync.runSync (node:internal/modules/esm/module_job:507:37)
at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:443:47)
at loadESMFromCJS (node:internal/modules/cjs/loader:1567:24)
at Module._compile (node:internal/modules/cjs/loader:1720:5)
at Object..js (node:internal/modules/cjs/loader:1871:10)
at Module.load (node:internal/modules/cjs/loader:1470:32)
at Module._load (node:internal/modules/cjs/loader:1290:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:238:24)

Node.js v24.7.0
file:///home/alifarooq/Documents/Workspace/cannabis-automation-be/dist/generated/prisma/client.js:38
Object.defineProperty(exports, "__esModule", { value: true });
^

ReferenceError: exports is not defined in ES module scope
at file:///home/alifarooq/Documents/Workspace/cannabis-automation-be/dist/generated/prisma/client.js:38:23
at ModuleJobSync.runSync (node:internal/modules/esm/module_job:507:37)
at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:443:47)
at loadESMFromCJS (node:internal/modules/cjs/loader:1567:24)
at Module._compile (node:internal/modules/cjs/loader:1720:5)
at Object..js (node:internal/modules/cjs/loader:1871:10)
at Module.load (node:internal/modules/cjs/loader:1470:32)
at Module._load (node:internal/modules/cjs/loader:1290:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:238:24)

Node.js v24.7.0
what am i doing wrong. any guidance/help is appreciated. thanks.
13 Replies
Prisma AI Help
Prisma AI Help3mo ago
Skipping coffee for instant knowledge! Let's brew that AI answer now. The error 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:
generator client {
provider = "prisma-client"
engineType = "client"
previewFeatures = ["relationJoins"]
output = "../generated/prisma"
moduleFormat = "esm"
}
generator client {
provider = "prisma-client"
engineType = "client"
previewFeatures = ["relationJoins"]
output = "../generated/prisma"
moduleFormat = "esm"
}
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.
Ali Farooq
Ali FarooqOP3mo ago
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"resolvePackageJsonExports": true,
"esModuleInterop": true,
"isolatedModules": true,
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2023",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"noFallthroughCasesInSwitch": false
}
}
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"resolvePackageJsonExports": true,
"esModuleInterop": true,
"isolatedModules": true,
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2023",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"noFallthroughCasesInSwitch": false
}
}
@Prisma AI Help this is my tsconfig.json
Nurul
Nurul3mo ago
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.
Eric
Eric3mo ago
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?
Gregersen
Gregersen3mo ago
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:
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();
}
}
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();
}
}
Does this solve your problem?
᲼;
᲼;3mo ago
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.
Gregersen
Gregersen3mo ago
Sorry, duplicating what? I'm not sure I follow 😄
᲼;
᲼;3mo ago
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.
Gregersen
Gregersen3mo ago
Thats exactly what I'm doing, but I recommended the same thing to OP. Did you mean to respond to OP?
᲼;
᲼;3mo ago
Please just think of this as me talking to myself.d( ̄  ̄) :aVerified:
Eric
Eric3mo ago
@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?
No description
Gregersen
Gregersen3mo ago
@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 😄
Gregersen
Gregersen3mo ago
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...

Did you find this page helpful?