prisma: `__dirname is not defined` when generating PrismaClient to custom location, w/ SvelteKit
Bug description
Prisma seems to have issues running with SvelteKit when generating the client to some other location other than the default.
When running a production build for the app, the following error will show:
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 '/<sveltekit-project-dir>/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///<sveltekit-project-dir>/build/server/chunks/2-6f7b7120.js:19960:27
at file:///<sveltekit-project-dir>/build/server/chunks/2-6f7b7120.js:31969:3
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async Promise.all (index 1)
at async render_page (file:///<sveltekit-project-dir>/build/server/index.js:2382:19)
at async resolve (file:///<sveltekit-project-dir>/build/server/index.js:2872:22)
at async respond (file:///<sveltekit-project-dir>/build/server/index.js:2910:22)
at async Array.ssr (file:///<sveltekit-project-dir>/build/handler.js:19060:3)
Notes:
- This issue does not happen when running
vite devorvite previewfor some reason. Prisma works as expected under those conditions. The error only happens when running the build output directly with node. - If you remove the
outputoption from the generator, then import the prisma client from ‘@prisma/client’, Prisma will run as expected - but unfortunately that doesn’t meet my needs. - Reason for this use case: I need to connect to multiple schemas, and I want the benefit of prisma’s generated types for both. As far as I know, the only way to connect to multiple schemas is to generate multiple prisma clients - which in turn means I can’t use ‘@prisma/client’ because that package will only point to one client at a time. (When generating multiple clients to the default location, they will just overwrite each other)
How to reproduce
See my test repo where I’ve reproduced the error: https://github.com/McGaelen/prisma-sveltekit
Expected behavior
No error and Prisma runs as expected.
Prisma information
generator client {
provider = "prisma-client-js"
output = "../node_modules/custom-client-location"
}
...(the rest is an exact copy of https://www.prisma.io/docs/getting-started/quickstart)
import {PrismaClient} from 'custom-client-location' // <- importing from custom location specified in prisma.schema
const prisma = new PrismaClient()
Environment & setup
- OS: macOS, CentOS
- Database: PostgreSQL, SQLite, but probably all of them
- Node.js version: v16.15.1
Prisma Version
prisma : 4.4.0
@prisma/client : 4.4.0
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine : migration-engine-cli f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine : introspection-core f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary : prisma-fmt f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Format Wasm : @prisma/prisma-fmt-wasm 4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6
Default Engines Hash : f352a33b70356f46311da8b00d83386dd9f145d6
Studio : 0.474.0
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 23
- Comments: 22 (2 by maintainers)
Commits related to this issue
- Hack to fix prisma with adapter-node https://github.com/prisma/prisma/issues/15614#issuecomment-1491269798 — committed to liloryan/iru_belongings by kavidey a year ago
I have the error
ReferenceError: __dirname is not defined in ES module scopeas well in SvelteKit but not with a custom location.Can confirm this is an issue we are affected with (Sveltekit + Prisma fails on node-adapter). Probably related to https://github.com/prisma/prisma/issues/5030#issuecomment-1347116391?
I’m getting this error too, but with
solid-start.Would be awesome if this would be fixed!
@feynmanliang I got a hacky solution from other thread that works
Is there no way to detect if __dirname exists and if not, shim/manage it with import.meta.url?
SvelteKit allows to build and ship the build folder “as is”, without needing to install any dependencies after the build.
https://github.com/prisma/prisma/issues/5030#issuecomment-1398076317 solution is not very clean, but it also forces to reinstall @prisma/client where the build is shipped, which is really not convenient!
It seems very strange to keep this __dirname “just for Vercel”
simillar problem with running binary executable produced by
bun build --compilestart error info
I am no longer having any issues with Prisma and not sure what changed. I don’t think I will have to run the prototype version-- but just out of curiosity I’ll try to make that happen in the next few days and report back any issues I run into.
@gtim mentioned in https://github.com/prisma/prisma/discussions/19564 that our prototype version with ESM support worked.
@feynmanliang @ryoppippi @dievardump @wvhulle @AlienHack @maietta @McGaelen @MilyMilo @apollo79 It would really help if you could try that prototype version, see instructions at https://pris.ly/esm
The more feedback we get in https://github.com/prisma/prisma/discussions/19564, the more confident we will be to bring this into the latest version of Prisma 🙌🏼
Linking the solution that @AlienHack mentioned: https://github.com/prisma/prisma/issues/5030#issuecomment-1398076317
It’s ugly - but it works. Would love to see some movement on this issues!