#package.json type module

4 messages · Page 1 of 1 (latest)

fervent notch
#

hi, I am upgrading to prisma 7 in my nest js project and in the migration guide they say set the following:

https://www.prisma.io/docs/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-7?utm_source=blog&utm_campaign=prisma7#esm-support

package.json

{
  "type": "module",
  "scripts": {...},
}

tsconfig.json

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "target": "ES2023",
    "strict": true,
    "esModuleInterop": true
  }
}

But when I try to run the project I get this error:

➜  poc_collector yarn translation
>  SWC  Running...
Successfully compiled: 224 files, copied 546 files with swc (300.25ms)
file:///Users/dennis/Projects/poc_collector/dist/main.js:2
Object.defineProperty(exports, "__esModule", {
                      ^

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/dennis/Projects/poc_collector/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/dennis/Projects/poc_collector/dist/main.js:2:23
    at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Is there a guide for es modules with nest js that I am missing?

Guide on how to upgrade to Prisma ORM 7

#

This is my full tsconfig:

{
  "compilerOptions": {

    "module": "ESNext",
    "moduleResolution": "node",
    "target": "ES2023",
    "strict": true,
    "esModuleInterop": true,

    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "forceConsistentCasingInFileNames": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "noFallthroughCasesInSwitch": false,
    "resolveJsonModule": true
  }
}
analog rune
#

Solutions for Prisma 7 with NestJS

There are two simple ways to fix compatibility issues between Prisma 7 and NestJS:

Method 1: Modifying tsconfig.json

Modify the following properties in your tsconfig.json:

{
  "module": "commonjs",
  "moduleResolution": "node",
  "resolvePackageJsonExports": false
}

Prisma Schema for Method 1

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

datasource db {
  provider = "postgresql"
}

Method 2: Only modifying schema.prisma

Add moduleFormat = "cjs" to the generator:

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

datasource db {
  provider = "postgresql"
}

Note: With Method 2, you don't need to modify tsconfig.json

Benefits

  • ✅ Works with Prisma 7
  • ✅ Compatible with NestJS
  • ✅ Unit tests work correctly
  • ✅ No version rollback required
  • Method 1: Unit tests work normally
  • ⚠️ Method 2: Unit tests may not work correctly

Recommendation

Use Method 1 if you need to run unit tests. Method 2 is simpler but may cause issues with tests.