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 4 years ago
- Reactions: 30
- Comments: 34 (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 3 years ago
- fix: hacky prisma issue fix https://github.com/prisma/prisma/issues/15614 — committed to marcuses101/svelte-billing by marcuses101 2 years ago
- Set up Dockerfile Includes an awful-awful-no-good workaround for getting Prisma and SvelteKit to work together in an ESM environment Tracking issue: https://github.com/prisma/prisma/issues/15614 — committed to PRIME-TU-Delft/graaf-svelte-4 by juliavdkris 2 years ago
I have the error
ReferenceError: __dirname is not defined in ES module scopeas well in SvelteKit but not with a custom location.I have created a shell script that updates the
build/handler.jsand adds the missing globals.fix_dirname_error.sh:then in the package.json update the build command:
"build": "vite build && ./fix_dirname_error.sh",The above works with the current (2.5.10) version of sveltekit and on linux. I assume would work on Mac too.
@feynmanliang I got a hacky solution from other thread that works
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?
Ok, I’ve found a solution for the “__dirname is not defined in ES module scope” error.
The issue arises when using ES modules (ESM) in the generated Prisma Client. The
__dirnamevariable is not automatically available in ESM.Solution:
Explicitly set
moduleFormat = "esm"in the generator block of yourschema.prismafile. This ensures that the Prisma Client is generated with the correct module format and usesimport.meta.urlinstead of__dirnamefor resolving file paths.ref : https://www.prisma.io/docs/orm/prisma-schema/overview/generators#field-reference-1
I’m getting this error too, but with
solid-start.Would be awesome if this would be fixed!
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”
ReferenceError: __dirname is not defined in ES module scope at file:///var/task/chunks/nitro/nitro.mjs:10417:16 at ModuleJob.run (node:internal/modules/esm/module_job:234:25) at async ModuleLoader.import (node:internal/modules/esm/loader:473:24) at async d (/opt/rust/nodejs.js:16:18341) Node.js process exited with exit status: 1. The logs above can help with debugging the issue. ReferenceError: __dirname is not defined in ES module scope at file:///var/task/chunks/nitro/nitro.mjs:10417:16 at ModuleJob.run (node:internal/modules/esm/module_job:234:25) at async ModuleLoader.import (node:internal/modules/esm/loader:473:24) at async d (/opt/rust/nodejs.js:16:18341) Node.js process exited with exit status: 1. The logs above can help with debugging the issue.Same with nuxt 3 / vercel
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!
The issue should be solved by migrating from
prisma-client-jsto the newprisma-client-generator and if need be settingmoduleFormat = "esm", see Generators in the Prisma docs. This is also what @Mateleo does in his post even though he does not explicitly mention it.I actually managed to solve that issue… simply by moving away from nodejs entirely (no hate & no rant).
We are using webpack in our backend, as we have a mix of JS, ES6 ant TS, and this resolved the issue:
webpack.config.ts
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 🙌🏼