Reputation: 61
I am developing API with nestjs and TypeORM connected with PostgreSQL. NestJS is using ormconfig.json
file to provide database connection.
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "xyz",
"password": "xyz",
"database": "xyz",
"entities": ["src/**/**.entity{.ts,.js}"],
"synchronize": true
}
When in dev mode npm run start:dev
everything works OK, API is responding, connected properly with database. But when I try run production build: npm run prestart:prod
followed by npm run start:prod
something breaks and program cannot connect to the database, that cause error:
[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;
}
I think that code itself is correct, but there is something wrong with proper configuration.
nestjs ver.6.0.0
Upvotes: 1
Views: 2066
Reputation: 44
try this one: https://github.com/nestjs/nest/issues/184 using a dynamic ormconfig.js (instead of .json)
Upvotes: 1