SvelteKit __dirname is not defined in ES module scope

ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at requireLibrary (file:///server/chunks/_server.ts-C8y8rTjF.js:51:5556)
at file:///server/chunks/_server.ts-C8y8rTjF.js:221:21
at requireClient (file:///server/chunks/_server.ts-C8y8rTjF.js:432:4)
at require_default$1 (file:///server/chunks/_server.ts-C8y8rTjF.js:442:34)
at require_default (file:///server/chunks/_server.ts-C8y8rTjF.js:453:21)
at file:///server/chunks/_server.ts-C8y8rTjF.js:458:37
at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
at async respond (file:///server/index.js:5460:22)
at async Array.ssr (file:///handler.js:1284:3)
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at requireLibrary (file:///server/chunks/_server.ts-C8y8rTjF.js:51:5556)
at file:///server/chunks/_server.ts-C8y8rTjF.js:221:21
at requireClient (file:///server/chunks/_server.ts-C8y8rTjF.js:432:4)
at require_default$1 (file:///server/chunks/_server.ts-C8y8rTjF.js:442:34)
at require_default (file:///server/chunks/_server.ts-C8y8rTjF.js:453:21)
at file:///server/chunks/_server.ts-C8y8rTjF.js:458:37
at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
at async respond (file:///server/index.js:5460:22)
at async Array.ssr (file:///handler.js:1284:3)
I have tried about 10 different solutions searching around and nothing works, I am using SvelteKit5 does anyone have a remedy for this???
5 Replies
Prisma AI Help
Well met, adventurer! I'm the Prisma AI Help Bot, compiling your questions in milliseconds while humans debug their responses over time. Who’s on your team?
(・ᴗ・ )
(・ᴗ・ )OP6d ago
here is how I am importing
import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
straight from the docs schema.prisma:
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Nurul
Nurul6d ago
I think this is a bug and there is a PR to get it fixed https://github.com/prisma/prisma/pull/26892
GitHub
fix(client-generator-ts): don't use __dirname in class.ts in ES...
The __dirname shim was only built in client.ts but we had one more reference in class.ts which broke when building for ESM.
(・ᴗ・ )
(・ᴗ・ )OP6d ago
@Nurul omg i really dont want to switch to drizzle how long do you think before these will be merged into a release? i mean i see the changes were approved, they aren't merged like how long do you think before they get actually merged
Nurul
Nurul6d ago
This should be fixed in next release in prisma version 6.7. It should be out in 2 weeks. Considering that the new prisma-client is still in early access, it needs a bit ironing out before it becomes the default in Prisma 7.

Did you find this page helpful?