I've been attempting to establish a Prisma client within my NestJS application, but I keep encountering this persistent error:
[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)
The code that's triggering the error looks like this:
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();
});
}
}
This is a list of dependencies being used:
"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"
},
I've attempted implementing the `new` keyword in various places, but it seems like there may be an issue with dependency injection within NestJS itself. Despite this being a seemingly straightforward example, I'm surprised nobody has identified the bug yet. My background lies in Java programming, where objectivity reigns supreme, so navigating this problem has proven more challenging than expected.