Files
Kokorone/.agents/skills/prisma-upgrade-v7/references/esm-support.md
Jeffery f44200f543 feat: 完成 B 群組 — 資料層與核心領域模型
- 導入 Prisma 7.9.1 + SQLite(better-sqlite3 driver adapter),prisma.config.ts 管理連線設定
- 完整資料模型:User/Work/Character/CharacterAlias/EpisodicMemory/SemanticMemory/
  ProceduralRule/EmotionState/Relationship/SentimentLedgerEntry
- packages/db:Prisma Client 存取層(ESM,因 Prisma 7 產出的 generated client 僅支援 ESM)
- apps/api 隨之改為 ESM 以相容 packages/db
- packages/shared:新增角色/記憶/情緒/關係共用型別,api 與 web 皆從此匯入
- prisma/seed.ts:建立測試使用者與元氣型測試角色,含完整關係帳本與記憶種子資料
- apps/api:新增 GET /characters
- scripts/smoke/B.mjs:驗證資料表齊全、API 讀出種子角色、關係與記憶可寫入讀出
- 保留 Prisma 官方隨 CLI 附的 agent skill 文件(.agents/skills 等),供後續群組查閱

npm run restart && npm run smoke -- B 皆通過(B-V),A 群組冒煙測試無回歸。
2026-08-13 09:43:15 +08:00

3.1 KiB

ESM and CommonJS Support

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.

ESM Projects

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/**/*"]
}

CommonJS Projects

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"
}

Generator Fields That Matter

  • moduleFormat: esm or cjs
  • runtime: nodejs, bun, deno, workerd, vercel-edge, react-native
  • generatedFileExtension: ts, mts, or cts
  • importFileExtension: ts, mts, cts, js, mjs, cjs, or empty

Example:

generator client {
  provider               = "prisma-client"
  output                 = "../generated/prisma"
  runtime                = "nodejs"
  moduleFormat           = "esm"
  generatedFileExtension = "ts"
  importFileExtension    = "ts"
}

Import Paths

Server Code

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

Browser-Safe Types

import { Prisma } from '../generated/prisma/browser'
import { Role } from '../generated/prisma/enums'
import type { UserModel } from '../generated/prisma/models/User'

File Extensions

With moduleResolution: "Node16" or "NodeNext", use .js/.mjs/.cjs extensions that match your emitted files.

With moduleResolution: "bundler", bare relative imports are usually fine.

Minimum Versions

Requirement Minimum Version
Node.js 20.19.0
TypeScript 5.4.0

Framework Considerations

Next.js

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

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.

Troubleshooting

"ERR_REQUIRE_ESM"

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.

"Cannot use import statement outside a module"

Your app is still being executed as CommonJS. Add "type": "module" or use moduleFormat = "cjs" instead.

TypeScript compilation errors

Ensure module, moduleResolution, and your generator's moduleFormat agree with one another.