Skip to content

__dirname is not defined when generating PrismaClient to custom location, w/ SvelteKit #15614

@McGaelen

Description

@McGaelen

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions