Chapter 09 · Prisma Upgrade V7
Subchapter 9.4
references/esm-support.mdMarkdown3 KBView on GitHub
Prisma ORM v7 is ESM-first, but the prisma-client generator can target either ESM or CommonJS. Use ESM by default, and opt into CommonJS with moduleFormat = "cjs" if your project still needs it.
Add "type": "module" to package.json and use an ESM-compatible tsconfig.json:
{
"type": "module",
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
}{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2023",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["src/**/*", "prisma/**/*"]
}If the rest of your app is still CommonJS, keep that setup and make the generated Prisma Client CommonJS too:
{
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"target": "ES2022",
"esModuleInterop": true
}
}generator client {
provider = "prisma-client"
output = "../generated/prisma"
moduleFormat = "cjs"
}moduleFormat: esm or cjsruntime: nodejs, bun, deno, workerd, vercel-edge, react-nativegeneratedFileExtension: ts, mts, or ctsimportFileExtension: ts, mts, cts, js, mjs, cjs, or emptyExample:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
runtime = "nodejs"
moduleFormat = "esm"
generatedFileExtension = "ts"
importFileExtension = "ts"
}import { PrismaClient } from '../generated/prisma/client'import { Prisma } from '../generated/prisma/browser'
import { Role } from '../generated/prisma/enums'
import type { UserModel } from '../generated/prisma/models/User'With moduleResolution: "Node16" or "NodeNext", use .js/.mjs/.cjs extensions that match your emitted files.
With moduleResolution: "bundler", bare relative imports are usually fine.
| Requirement | Minimum Version |
|---|---|
| Node.js | 20.19.0 |
| TypeScript | 5.4.0 |
Next.js works well with the default ESM output. If you need generated types in client components, import them from browser, models, or enums, not from client.
Bun loads .env files automatically, so ESM plus env() is the smoothest default. You can still choose moduleFormat = "cjs" if the rest of your project requires it.
Your generated client is ESM, but your app is requiring it as CommonJS. Either switch the project to ESM or set moduleFormat = "cjs" and regenerate.
Your app is still being executed as CommonJS. Add "type": "module" or use moduleFormat = "cjs" instead.
Ensure module, moduleResolution, and your generator’s moduleFormat agree with one another.