I'm running a NestJS server with a Postgres database. The NestJS server works fine locally, but in production, it always results in the following error:
我正在运行一台带有Postgres数据库的NestJS服务器。NestJS服务器在本地运行良好,但在生产中,它总是导致以下错误:
[Nest] 20 - 09/05/2023, 8:43:09 PM ERROR [ExceptionsHandler] Cyclic dependency: "d"
TypeORMError: Cyclic dependency: "d"
at visit (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:147:23)
at visit (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:164:21)
at SubjectTopoligicalSorter.toposort (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:143:17)
at SubjectTopoligicalSorter.sort (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:53:45)
at SubjectExecutor.execute (/app/node_modules/typeorm/persistence/SubjectExecutor.js:91:108)
at EntityPersistExecutor.execute (/app/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:36)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I connected my local development server to my remote production Postgres database, and it works fine. Only when my production server connects to the production Postgres database - then I get the error above.
我将本地开发服务器连接到远程生产Postgres数据库,它工作得很好。只有当我的生产服务器连接到生产Postgres数据库时,我才会收到上面的错误。
My server is a NestJS server running with the following stack:
我的服务器是运行以下堆栈的NestJS服务器:
Node: 16.19.1
Package Manager: npm 8.19.3
OS: darwin arm64
"dependencies": {
"@angular/animations": "^13.0.0",
"@angular/cdk": "^13.0.0",
"@angular/common": "^13.0.0",
"@angular/compiler": "^13.0.0",
"@angular/core": "^13.0.0",
"@angular/forms": "^13.0.0",
"@angular/platform-browser": "^13.0.0",
"@angular/platform-browser-dynamic": "^13.0.0",
"@angular/router": "^13.0.0",
"@nestjs/common": "^8.0.0",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/jwt": "^10.0.3",
"@nestjs/mapped-types": "^2.0.2",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/serve-static": "^4.0.0",
"@nestjs/typeorm": "^10.0.0",
"@nrwl/angular": "13.3.1",
"bootstrap": "^5.2.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"cloudinary": "^1.37.3",
"express": "^4.18.2",
"http-status-codes": "^2.2.0",
"multer": "^1.4.5-lts.1",
"pg": "^8.11.1",
"primeflex": "^3.3.0",
"primeicons": "^4.1.0",
"primeng": "^13.4.1",
"reflect-metadata": "^0.1.13",
"rxjs": "~7.4.0",
"sqlite3": "^5.1.6",
"tslib": "^2.0.0",
"typeorm": "^0.3.17",
"uuid": "^9.0.0",
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "~13.0.0",
"@angular-eslint/eslint-plugin": "~13.0.1",
"@angular-eslint/eslint-plugin-template": "~13.0.1",
"@angular-eslint/template-parser": "~13.0.1",
"@angular/cli": "^13.3.4",
"@angular/compiler-cli": "^13.0.0",
"@angular/language-service": "^13.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@nrwl/cli": "13.3.1",
"@nrwl/cypress": "13.3.1",
"@nrwl/eslint-plugin-nx": "13.3.1",
"@nrwl/jest": "13.3.1",
"@nrwl/linter": "13.3.1",
"@nrwl/nest": "13.3.1",
"@nrwl/node": "13.3.1",
"@nrwl/tao": "13.3.1",
"@nrwl/workspace": "13.3.1",
"@types/express": "^4.17.17",
"@types/jest": "27.0.2",
"@types/multer": "^1.4.7",
"@types/node": "^15.14.0",
"@typescript-eslint/eslint-plugin": "~5.3.0",
"@typescript-eslint/parser": "~5.3.0",
"cypress": "^8.3.0",
"eslint": "8.2.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-cypress": "^2.10.3",
"jest": "27.2.3",
"jest-preset-angular": "11.0.0",
"prettier": "^2.3.1",
"ts-jest": "27.0.5",
"typescript": "~4.4.3"
}
And I have the following entities:
我有以下实体:
@Entity()
export class PatientEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
@Column({ type: 'date' })
dateOfBirth: Date;
@Column({ unique: true })
email: string;
@Column({ nullable: true })
phoneNumber: string;
@Column({ nullable: true })
photo: string | null;
@OneToMany(
() => MedicalPlanEntity,
plan => plan.patient,
{ cascade: true }
)
plans: MedicalPlanEntity[];
}
and
和
@Entity()
export class MedicalPlanEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ nullable: true })
name: string;
@Column({ nullable: true })
description: string;
@ManyToOne(
() => PatientEntity,
patient => patient.plans,
{ nullable: true }
)
patient: PatientEntity;
}
and my orm.config file is as follows:
我的orm.config文件如下所示:
import { registerAs } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
export default registerAs(
'orm.config',
(): TypeOrmModuleOptions => ({
ssl: true,
extra: {
ssl: { rejectUnauthorized: false }
},
type: 'postgres',
autoLoadEntities: true,
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
);
Any idea why in production I have the error above, while it works fine locally?
你知道为什么在生产中我会出现上面的错误,而它在本地运行得很好吗?
更多回答
After spending some days on this, it appeared that there were incompatibility issues between NestJS 8 and TypeOrm in production mode.
经过几天的讨论,NestJS8和TypeOrm在生产模式下似乎存在不兼容的问题。
The letter d
in the strange and untraceable error I was getting: TypeORMError: Cyclic dependency: "d"
- was a minified reference of one of my entities, that only appeared in production because the code is minified and optimized only in production.
我收到的奇怪且无法跟踪的错误中的字母d:TypeORMError:Cycle Dependency:“d”-是我的一个实体的缩小引用,它只出现在生产中,因为代码只在生产中缩小和优化。
To solve this, you have to upgrade to the most recent stack of nestjs and typeORM, or
要解决这个问题,您必须升级到最新的nestjs和typeORM堆栈,或者
only as a workaround, you can turn optimization to false
in production, and the error will go away:
作为一种解决办法,您可以在生产中将优化设置为FALSE,这样错误就会消失:
"configurations": {
"production": {
"optimization": false, // <--- false
"extractLicenses": true,
"inspect": false,
// ...
]
}
}
更多回答
我是一名优秀的程序员,十分优秀!