nestjs-realworld-example-app icon indicating copy to clipboard operation
nestjs-realworld-example-app copied to clipboard

npm run start:prod error

Open Darcy-shinet opened this issue 5 years ago • 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/[email protected]@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';

Darcy-shinet avatar Jun 09 '19 01:06 Darcy-shinet

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
}

ilyasfoo avatar Sep 24 '19 11:09 ilyasfoo

+1 any else answers?

eating-noodles avatar May 12 '20 07:05 eating-noodles

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.

eating-noodles avatar May 12 '20 08:05 eating-noodles