You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS集成Prisma报错:Class constructor t无法无new调用

NestJS中Prisma Client实例化报错:Class constructor t cannot be invoked without 'new'

错误信息

[Nest] 14352  - 12.05.2023, 23:21:13   ERROR [ExceptionHandler] Class constructor t cannot be invoked without 'new'

TypeError: Class constructor t cannot be invoked without 'new'                                                      
    at new PrismaService (C:\work\js\cyno-desu\dist\prisma\prisma.service.js:66:42)                                
    at Injector.instantiateClass (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:351:19)
    at callback (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:56:45)
    at Injector.resolveConstructorParams (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:136:24)
    at Injector.loadInstance (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:61:13)
    at Injector.loadProvider (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:88:9)
    at Injector.lookupComponentInImports (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:281:17)
    at Injector.lookupComponentInParentModules (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:245:33)
    at Injector.resolveComponentInstance (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:200:33)
    at resolveParam (C:\work\js\cyno-desu\node_modules\@nestjs\core\injector\injector.js:120:38)

触发错误的代码

import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }

  async enableShutdownHooks(app: INestApplication) {
    this.$on('beforeExit', async () => {
      await app.close();
    });
  }
}

项目依赖版本

"devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.5.0",
    "@types/node": "18.15.11",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.5.0",
    "prettier": "^2.3.2",
    "prisma": "^4.13.0",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.2.0",
    "typescript": "^4.7.4"
  }

解决方案

这个错误的核心原因是TypeScript编译配置不兼容Prisma Client的ES6类实现:Prisma Client采用原生ES6类编写,若项目编译目标设为ES5,TypeScript会将类转换为ES5的函数模拟形式,导致继承时无法正确调用PrismaClient的构造函数。

主解决步骤

  1. 打开项目根目录的tsconfig.json文件
  2. 修改compilerOptions中的target字段为ES6或更高版本(推荐ES2020,适配Node.js 14+)
  3. 确保compilerOptions中的lib数组包含对应目标版本的库(比如["ES2020", "DOM"])
  4. 重新编译并启动项目:
    npm run build
    npm run start:prod
    

备选方案(若编译配置修改无效)

不继承PrismaClient,改为在PrismaService内部实例化:

@Injectable()
export class PrismaService implements OnModuleInit {
  private prisma: PrismaClient;

  constructor() {
    this.prisma = new PrismaClient();
  }

  async onModuleInit() {
    await this.prisma.$connect();
  }

  // 暴露PrismaClient的所有方法
  get client() {
    return this.prisma;
  }

  async enableShutdownHooks(app: INestApplication) {
    this.prisma.$on('beforeExit', async () => {
      await app.close();
    });
  }
}

使用时通过prismaService.client访问Prisma的方法。


内容的提问来源于stack exchange,提问作者Bartłomiej Błaszczyk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:44:58