Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: __dirname is not defined in ES module scope when generating custom clients #22827

Open
danieltamas opened this issue Jan 26, 2024 · 5 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: esm topic: multiple clients topic: nuxt topic: output topic: vite

Comments

@danieltamas
Copy link

Bug description

Using Nuxt and Vite with prisma. In dev mode, everything works as expected.
However, when building the above error is fatal and stops the build.
For the moment there is no workaround that works and want to understand
what are the options to fix this.

Inspecting the culprits it looks like the __dirname and __filename usages
are present in the prisma:engines blocks.

Example below:

B.default.join(__dirname, "../query-engine-darwin");
B.default.join(__dirname, "../query-engine-darwin-arm64");
B.default.join(__dirname, "../query-engine-debian-openssl-1.0.x");
B.default.join(__dirname, "../query-engine-debian-openssl-1.1.x");
B.default.join(__dirname, "../query-engine-debian-openssl-3.0.x");
B.default.join(__dirname, "../query-engine-linux-static-x64");
B.default.join(__dirname, "../query-engine-linux-static-arm64");
B.default.join(__dirname, "../query-engine-rhel-openssl-1.0.x");
B.default.join(__dirname, "../query-engine-rhel-openssl-1.1.x");
B.default.join(__dirname, "../query-engine-rhel-openssl-3.0.x");

How to reproduce

  1. Install Nuxt with Vite ( nuxt: 3.9.3, nitro: 2.8.1, vite: 5.0.11 )
  2. Create extra prisma schemas in the /prisma/ folder ie: /prisma/my_service.prisma
  3. Generate the client npx prisma generate --schema prisma/my_service.prisma
  4. Build the app using yarn build
  5. Die a slow death from bugs

Expected behavior

The app should build and start in the node environment

Prisma information

Client 1

generator client {
  provider        = "prisma-client-js"
  output          = "client/ams"
}

datasource ams {
  provider = "postgresql"
  url      = env("AMS_DATABASE_URL")
} 

Client 2

generator client {
  provider = "prisma-client-js"
  output = "client/cockroach"
}

datasource cockroach {
  provider = "cockroachdb"
  url      = env("COCKROACH_DATABASE_URL")
}

Environment & setup

  • OS: [macos(Ventura 13.4), linux(Debian 12)]
  • Database: [PostgreSQL, CockroachDB]
  • Node.js version: [20.0.9]

Prisma Version

5.8.1
@danieltamas danieltamas added the kind/bug A reported bug. label Jan 26, 2024
@Druue Druue added topic: esm topic: output topic: multiple clients domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jan 29, 2024
@janpio janpio changed the title ReferenceError: __dirname is not defined in ES module scope when generating custom clients ReferenceError: __dirname is not defined in ES module scope when generating custom clients Feb 19, 2024
@jkomyno jkomyno self-assigned this Mar 1, 2024
@jkomyno
Copy link
Contributor

jkomyno commented Mar 1, 2024

Hi @danieltamas, I tried reproducing your issue. I assume the Prisma schemas you pasted above are different than the ones you had locally, as running prisma generate with them results in the following error:

❯ pnpm prisma generate --schema ./prisma/my_service.prisma
Environment variables loaded from .env
Prisma schema loaded from prisma/my_service.prisma
Error: 
You don't have any models defined in your schema.prisma, so nothing will be generated.
You can define a model like this:

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

More information in our documentation:
https://pris.ly/d/prisma-schema

I've thus added the following as an example model to both schemas:

model User {
  id    Int     @id
  email String  @unique
  name  String?
}

Besides this, I was not able to reproduce this issue. Can you please double check that your reproduction list is exhaustive? I've even tried to add two custom server entries:

// ./server/api/cockroach.ts

import { PrismaClient } from '../../prisma/client/cockroach'

export default defineEventHandler((event) => {
  const prisma = new PrismaClient()
  return {
    hello: 'cockroach'
  }
})
// ./server/api/ams.ts

import { PrismaClient } from '../../prisma/client/ams'

export default defineEventHandler((event) => {
  const prisma = new PrismaClient()
  return {
    hello: 'ams'
  }
})
Screenshot 2024-03-01 at 11 42 46

@Floppy012
Copy link

@jkomyno I just ran into this error. I've uploaded a reproduction repository https://github.com/floppy012-repros/nuxt-prisma

@sarat1669
Copy link

Bump. Any update on this?

@jkomyno
Copy link
Contributor

jkomyno commented May 7, 2024

I have reproduced this with https://github.com/floppy012-repros/nuxt-prisma.

Observations:

  • Nuxt 3 uses ESM natively, but Prisma isn't natively ESM-compatible yet
  • One can still run Prisma with nuxt dev by transpiling it via
    // nuxt.config.js
    //
    // https://nuxt.com/docs/api/configuration/nuxt-config
    export default defineNuxtConfig({
      devtools: { enabled: true },
      build: {
        transpile: ['~/prisma/client'], // <-- add this
      },
    })
  • You might also consider following up on RC9 Dependency Break - CJS Imports nuxt/nuxt#14790 threads for the time being

@jkomyno jkomyno added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 7, 2024
@jkomyno jkomyno removed their assignment May 7, 2024
@sarat1669
Copy link

Does anyone have a working example of prisma + qwikjs (non-edge)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: esm topic: multiple clients topic: nuxt topic: output topic: vite
Projects
None yet
Development

No branches or pull requests

6 participants