1

I made a small nestjs webapp, it connects to a local mssql database and works fine... Except if I try to launch it using pm2.

  • regular yarn start:dev works (it translates to nest start --watch)
  • building the production and running yarn start:prod also works fine (it translates to node dist/main)
  • running nodemon build/main.js also works fine

But if I try to launch my app running pm2 start build/main.js I get DB connection errors: TypeError: The "config.server" property is required and must be of type string.

My DB connection parameters are currently stored in a .env file. I load them running env.config() and it's working fine for the other three running methods I listed. I've tried to change the parameters to include quotes and double quotes, with no luck:

  • db_host = localhost
  • db_host = 'localhost'
  • db_host = "localhost"

What am I doing wrong?

Bruno Lamps
  • 544
  • 6
  • 27
  • From where the `env.config()` method is comming exactly? did you have more than one `.env`? (btw I'm using locally with PM2) – Micael Levi Feb 22 '21 at 17:37
  • @MicaelLevi in my application's root there's only one .env file, all parameters come from this file – Bruno Lamps Feb 22 '21 at 17:40
  • I just learned that this application won't work using this syntax, despite nodemon running normally. I had to use a syntax like `pm2 start npm --name "myApp" -- run start:prod` to start it correctly. – Bruno Lamps Feb 22 '21 at 18:29
  • oh right. In my app I was building it (via `nest build` in [`pre` hook](https://docs.npmjs.com/cli/v7/using-npm/scripts#pre--post-scripts)) before running `pm2 start` (which will run `node dist/src/main.js`) and not relying on `start:prod` npm-script – Micael Levi Feb 22 '21 at 20:15

2 Answers2

2

We just found out that changing the syntax from pm2 start to something like pm2 start npm --name "myApp" -- run start:prod worked.

Bruno Lamps
  • 544
  • 6
  • 27
0

This can help you, in the documentation says you can use the environment variables in ecosystem.config.js file: https://pm2.keymetrics.io/docs/usage/environment/