ReferenceError: __dirname is not defined in ES module scope

prisma.ts import { PrismaClient } from '../../generated/prisma' const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined } const prisma = globalForPrisma.prisma ?? new PrismaClient({ log: ['query', 'error', 'warn'], }) if (process.env.NODEENV !== 'production') globalForPrisma.prisma = prisma export default prisma; visual code C:\Users\nguye\Desktop\project6>node .output/server/index.mjs file:///C:/Users/nguye/Desktop/project6/.output/server/chunks//nitro.mjs:6266 M.default.join(dirname, "../query-engine-darwin"); ^ ReferenceError: dirname is not defined in ES module scope at file:///C:/Users/nguye/Desktop/project6/.output/server/chunks/_/nitro.mjs:6266:16 at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async loadESM (node:internal/process/esm_loader:34:7) at async handleMainPromise (node:internal/modules/run_main:106:12) Node.js v18.20.8
4 Replies
Prisma AI Help
You chose to compile with the bot. One moment while I piece together your answer… The error ReferenceError: dirname is not defined in ES module scope occurs because, in ES modules (when using "type": "module" in package.json or .mjs files), the dirname variable is not available. Prisma Client, by default, uses dirname internally to resolve paths, which leads to this issue when running in ESM environments like your Nuxt/Nitro or Node.js ESM setup. Solution:
You need to generate the Prisma Client in ESM mode so it uses import.meta.url instead of
dirname for path resolution. To do this, add moduleFormat = "esm" to your generator block in schema.prisma:
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
moduleFormat = "esm" // Ensures ESM-compatible client
}
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
moduleFormat = "esm" // Ensures ESM-compatible client
}
After updating schema.prisma, run npx prisma generate to regenerate the client. This should resolve the dirname error in ESM environments.
See discussion and workaround Additional notes: - If you are bundling your app (e.g., with Nitro, Webpack, or esbuild), make sure to externalize @prisma/client and .prisma so they are not bundled, as bundlers may not handle native modules or dynamic requires well. For Nuxt/Nitro, you can add to your config:
externals: {
external: [
".prisma",
"@prisma/client",
"process",
],
inline: [],
}
externals: {
external: [
".prisma",
"@prisma/client",
"process",
],
inline: [],
}
See user workaround If you still encounter issues, ensure you are not mixing CJS and ESM code, and that your deployment copies all necessary Prisma files. Summary:
Set moduleFormat = "esm" in your Prisma generator, regenerate the client, and ensure bundler externals are set for @prisma/client and .prisma. This will resolve the
dirname error in ESM environments.
Prisma AI Help
🤔 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.
Nurul
Nurul2w ago
Can you change the generator from prisma-client-js to prisma-client and check?

Did you find this page helpful?