lujakob/nestjs-realworld-example-app

npm run start:prod error

Darcy-shinet opened this issue · 3 comments

I want to start prod this following error.
[Nest] 92672 - 2019-06-09 09:49 [NestFactory] Starting Nest application...
[Nest] 92672 - 2019-06-09 09:49 [InstanceLoader] TypeOrmModule dependencies initialized +131ms
[Nest] 92672 - 2019-06-09 09:49 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +105ms
/Users/lee/Sites/demo/nestjs-realworld-example-app/src/article/article.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column, OneToOne, ManyToOne, OneToMany, JoinColumn, AfterUpdate, BeforeUpdate } from 'typeorm';
^^^^^^

SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Function.PlatformTools.load (/Users/lee/Sites/demo/nestjs-realworld-example-app/node_modules/_typeorm@0.2.18@typeorm/platform/PlatformTools.js:107:28)
[Nest] 92672 - 2019-06-09 09:49 [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3025ms
/Users/lee/Sites/demo/nestjs-realworld-example-app/src/article/article.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column, OneToOne, ManyToOne, OneToMany, JoinColumn, AfterUpdate, BeforeUpdate } from 'typeorm';

Try this:

In package.json modify npm scripts to include NODE_ENV environment variable

{
...
  "scripts": {
    "start": "NODE_ENV=development node index.js",
    "start:watch": "NODE_ENV=development nodemon",
    "start:prod": "NODE_ENV=production node dist/src/main.js",
   }
...
}

Change ormconfig.json to ormconfig.js and do something similar to the following. Note the entities now loads either .js or .ts according to environment.

module.exports = {
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "...",
  "password": "...",
  "database": "...",
  "entities": [ process.env.NODE_ENV == 'production' ? __dirname + '/dist/src/**/**.entity.js' : __dirname + '/src/**/**.entity.ts' ],
  "migrationsTableName": "migrations",
  "migrations": ["database/migration/*.js"],
	"cli": {
	    "migrationsDir": "database/migration"
	},
  "synchronize": true
}

+1 any else answers?

Try this:

In package.json modify npm scripts to include NODE_ENV environment variable

{
...
  "scripts": {
    "start": "NODE_ENV=development node index.js",
    "start:watch": "NODE_ENV=development nodemon",
    "start:prod": "NODE_ENV=production node dist/src/main.js",
   }
...
}

Change ormconfig.json to ormconfig.js and do something similar to the following. Note the entities now loads either .js or .ts according to environment.

module.exports = {
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "...",
  "password": "...",
  "database": "...",
  "entities": [ process.env.NODE_ENV == 'production' ? __dirname + '/dist/src/**/**.entity.js' : __dirname + '/src/**/**.entity.ts' ],
  "migrationsTableName": "migrations",
  "migrations": ["database/migration/*.js"],
	"cli": {
	    "migrationsDir": "database/migration"
	},
  "synchronize": true
}

it works.Just remember to modify the workspace.