无法运行生产版本

时间:2019-04-09 20:49:41

标签: nestjs

我正在使用与PostgreSQL连接的nestjs和TypeORM开发API。 NestJS正在使用ormconfig.json文件来提供数据库连接。

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "xyz",
  "password": "xyz",
  "database": "xyz",
  "entities": ["src/**/**.entity{.ts,.js}"],
  "synchronize": true
}

在开发人员模式npm run start:dev下,一切正常,API响应,并与数据库正确连接。但是,当我尝试运行生产版本:npm run prestart:prod后跟npm run start:prod时,发生了某些故障,程序无法连接到数据库,从而导致错误:

[Nest] 12444   - 2019-04-09 22:19   [NestFactory] Starting Nest application...
[Nest] 12444   - 2019-04-09 22:19   [InstanceLoader] TypeOrmModule dependencies initialized +85ms
[Nest] 12444   - 2019-04-09 22:19   [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 12444   - 2019-04-09 22:19   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +111ms
C:\_code\sensorhub\packages\api-nestjs\src\installations\models\installation.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
[Nest] 12444   - 2019-04-09 22:19   [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3130ms

installation.entity.ts

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class InstallationEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 500 })
  name: string;

  @Column('text')
  description: string;
}

我认为代码本身是正确的,但是正确的配置存在一些问题。

nestjs ver.6.0.0

1 个答案:

答案 0 :(得分:1)

尝试以下方法:https://github.com/nestjs/nest/issues/184 使用动态ormconfig.js(而不是.json)