【问题标题】:Cannot run production build无法运行生产构建
【发布时间】:2019-08-31 05:43:57
【问题描述】:

我正在使用nestjs 和TypeORM 与PostgreSQL 连接开发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;
}

我认为代码本身是正确的,但是正确的配置有问题。

nestjsver.6.0.0

【问题讨论】:

  • 尝试将"entities": ["src/**/**.entity{.ts,.js}"]改为"entities": ["src/**/**.entity.ts"]
  • @shusson,不幸的是还是同样的问题。

标签: nestjs


【解决方案1】:

试试这个:https://github.com/nestjs/nest/issues/184 使用动态 ormconfig.js(而不是 .json)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2021-11-17
    • 2018-05-27
    • 2021-07-07
    • 1970-01-01
    • 2018-06-20
    • 2020-02-02
    相关资源
    最近更新 更多