You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
prisma-client generator infers moduleFormat esm for CommonJS projects using module nodenext #29710
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
package.json without"type": "module"
tsconfig.json with "module": "nodenext", "moduleResolution": "nodenext"
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.
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.
Activity
fix(client): infer moduleFormat correctly for nodenext/node16 in CJS …
fix(client): infer moduleFormat correctly for nodenext/node16 in CJS …
ShauryaaSharma commentedon Jul 10, 2026
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 nearestpackage.jsonto the generator's output directory and infer"esm"only if its"type"field is"module", defaulting to"cjs"otherwise, mirroring howtscitself resolves module format per-file under these settings.commonjsand other explicit ESM-onlymodulevalues are unaffected.fix(client): infer moduleFormat correctly for nodenext/node16 in CJS …
fix(client): infer moduleFormat correctly for nodenext/node16 in CJS …