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 dev or vite preview for 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 output option 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

Most upvoted comments

I have the error ReferenceError: __dirname is not defined in ES module scope as well in SvelteKit but not with a custom location.

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 '/home/dsp/git/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/dsp/git/build/server/chunks/db-46c26d2c.js:19959:27
    at file:///home/dsp/git/build/server/chunks/db-46c26d2c.js:31968:3
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
    at async Promise.all (index 0)
    at async render_page (file:///home/dsp/git/build/server/index.js:1475:19)
    at async resolve (file:///home/dsp/git/build/server/index.js:1965:22)
    at async Object.handle (file:///home/dsp/git/build/server/chunks/hooks.server-62fe1b4e.js:3:20)
    at async respond (file:///home/dsp/git/build/server/index.js:2003:22)

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.

file:///<path_to_project>/dist/server.js:36641
        import_path2.default.join(__dirname, "../query-engine-darwin");
                                  ^

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 '<path_to_project>/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///<path_to_project>/dist/server.js:36641:28
    at file:///<path_to_project>/dist/server.js:52455:3
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)

Would be awesome if this would be fixed!

FWIW we have since migrated off of SvelteKit + Prisma as being locked in to Vercel for deployments did not meet our requirements.

@feynmanliang I got a hacky solution from other thread that works

5CEEE39C-FA31-4017-A08B-E13BD401275C 4A36CE88-8D8C-4D83-896D-AA448E80241B

As you are likely aware, the root cause seems to be that we’re using ES2022 but the Prisma client is using an old __dirname() function that isn’t support/available in ES2022, which I believe is the default used in SvelteKit. The reason for the continued use of this __dirname() function for the detection of the environment specifically for Vercel hosted websites.

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 --compile

start error info

prisma:error Can't find variable: __dirname
1 | __dirname
   ^
ReferenceError: Can't find variable: __dirname
      at compiled://root/test-elysia:4097:69
      at processTicksAndRejections (:1:2602)

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!