Skip to content

prisma-client generator infers moduleFormat esm for CommonJS projects using module nodenext #29710

Closed

Description

@samchon

Bug description

The prisma-client generator decides the default moduleFormat by looking only at compilerOptions.module in tsconfig.json: anything that isn't literally "commonjs" gets treated as ESM. But "module": "nodenext" doesn't mean the project is ESM. With nodenext, TypeScript decides the format per file from the nearest package.json type field, and when there's no "type": "module" everything compiles as CommonJS.

So in a CJS project using nodenext (a pretty common setup for NestJS backends), the generated client contains import.meta.url, tsc compiles it as CommonJS, and the compiled output blows up the moment it gets required:

ReferenceError: exports is not defined in ES module scope
    at .../src/prisma/client.ts:48:23

Same crash as #27556, which was closed with the moduleFormat = "cjs" workaround. The workaround works, but the default is still wrong.

What makes it nasty is that the generated files start with // @ts-nocheck, which suppresses the TS1470 error ('import.meta' is not allowed in a CommonJS file) that would otherwise catch the mismatch at compile time. So there is zero signal until it crashes at runtime, and the stack trace points at a generated file with a sourcemapped position in the middle of a doc comment, which makes it fun to debug.

How to reproduce

  1. package.json without "type": "module"
  2. tsconfig.json with "module": "nodenext", "moduleResolution": "nodenext"
  3. generator block without an explicit moduleFormat:
generator client {
  provider = "prisma-client"
  output   = "../src/prisma"
}
  1. prisma generate, compile with tsc, require() the generated client → crash above.

Expected behavior

When module is nodenext/node16, infer the way tsc does: look for "type": "module" in the nearest package.json and default to "cjs" when it's absent. Failing that, refusing to generate and asking for an explicit moduleFormat would also be fine. Anything but silently emitting ESM-only code into a CommonJS compilation.

Prisma information

datasource db {
  provider = "sqlite"
}

generator client {
  provider = "prisma-client"
  output   = "../../src/prisma"
}

Environment & setup

  • OS: Windows 11 / ubuntu-latest (GitHub Actions), reproduced on both
  • Database: SQLite (@prisma/adapter-better-sqlite3)
  • Node.js: 24.x
  • TypeScript: 7.0.2

Prisma Version

7.7.0 (checked the inference logic in the 7.8.0 bundle as well, same behavior)

Activity

  1. added 2 commits that reference this issue on Jul 10, 2026
    6f38b1a
    86e2fbc
  2. ShauryaaSharma commented on Jul 10, 2026

    @ShauryaaSharma
    Contributor

    Hey I have opened a fix : #29712

    What i did is that for module: "node16" / "nodenext", instead of defaulting to ESM, we now look up the nearest package.json to the generator's output directory and infer "esm" only if its "type" field is "module", defaulting to "cjs" otherwise, mirroring how tsc itself resolves module format per-file under these settings. commonjs and other explicit ESM-only module values are unaffected.

  3. added a commit that references this issue on Jul 22, 2026
    e48a14f
  4. added a commit that references this issue on Aug 9, 2026
    05d4053
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions