>我正在配置nestjs ormconfig.json,但typeorm不连接到MySQL 8数据库,它在CLI中显示它
[Nest] 13324 - 2020-01-14 4:15:32 [NestFactory] Starting Nest application...
[Nest] 13324 - 2020-01-14 4:15:32 [InstanceLoader] AppModule dependencies initialized +513ms
[Nest] 13324 - 2020-01-14 4:15:32 [InstanceLoader] TypeOrmModule dependencies initialized +5ms
[Nest] 13324 - 2020-01-14 4:15:36 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +4061ms
Error: Cannot find module 'src/user/user.entity'
Require stack:
- C:UsersredwolfLabsprojets-prokissing-apisrcappelappel.entity.ts
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormplatformPlatformTools.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormdriversqlserverSqlServerDriver.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormmigrationMigrationExecutor.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormconnectionConnection.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormconnectionConnectionManager.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modulestypeormindex.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modules@nestjstypeormdistcommontypeorm.utils.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modules@nestjstypeormdistcommontypeorm.decorators.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modules@nestjstypeormdistcommonindex.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modules@nestjstypeormdistindex.js
- C:UsersredwolfLabsprojets-prokissing-apinode_modules@nestjstypeormindex.js
- C:UsersredwolfLabsprojets-prokissing-apisrcapp.module.ts
- C:UsersredwolfLabsprojets-prokissing-apisrcmain.ts
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:UsersredwolfLabsprojets-prokissing-apisrcappelappel.entity.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Module.m._compile (C:UsersredwolfLabsprojets-prokissing-apinode_modulests-nodesrcindex.ts:806:23)
at Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Object.require.extensions.<computed> [as .ts] (C:UsersredwolfLabsprojets-prokissing-apinode_modulests-nodesrcindex.ts:809:12)
at Module.load (internal/modules/cjs/loader.js:815:32)
所有实体都在 SRC 文件夹中,但控制台仍然说不是
这是我的代码
app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot(),
],
})
export class AppModule {}
ormconfig.json
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "admin",
"password": "",
"database": "kissing_db",
"synchronize": true,
"logging": true,
"entities": [
"src/**/*.entity.ts",
"dist/**/*.entity.js"
],
"migrationsTableName": "migration",
"migrations": [
"src/migration/*.ts"
],
"cli": {
"migrationsDir": "./src/migration"
},
"ssl": false
}
出于安全原因,我不使用密码发布它,希望您能理解。
从 Typescript 编译到 JavaScript 时,您的代码会从src
目录中的.ts
文件移动到dist
目录中的.js
文件。从那里,Node 仍然尝试解析从基目录给出的路径(一旦dist
基目录dist
(,因此它会尝试解析显然不存在的dist/src/user/user.entity
。相反,您应该从ormconfig.json
中删除src/**/*.entity.ts
,以便它只查找已编译的 JavaScript 文件。
这里有更多信息。
我想我已经找到了一个解决方案,我只是创建了一个没有像这样的entities
字段的ormconfig.js
module.exports = {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "admin",
"password": "",
"database": "kissing_db",
"synchronize": true,
"logging": true,
};
并编辑我的package.json
用ts-node
启动脚本,似乎ts节点会自动找到实体文件。