Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I know that there are answers for this question, but I don't want to create one more config file and load all the configuration there and run the pm2 process.

Project Structure
-----------------
index.js -> server is listening in this file
routes/
models/
middleware/
startup/
package.json

Inside package.json

"name": "eventbooking", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node -r dotenv/config index.js" "keywords": [], "author": "", "license": "ISC", "dependencies": { "@hapi/joi": "^15.0.3", "bcryptjs": "^2.4.3", "compression": "^1.7.4", "dotenv": "^8.0.0", "express": "^4.17.1", "express-async-errors": "^3.1.1", "helmet": "^3.18.0", "joi-objectid": "^2.0.0", "jsonwebtoken": "^8.5.1", "mongoose": "^5.5.14", "winston": "^3.2.1"

As you see from my package.json file that I am loading the node -r dotenv/config index.js file from scripts > start

When I run locally with the following command

npm start

The project works totally fine.

Now I have deployed the project to the server and there if I manually run

npm start

then works fine.

When I install PM2 in Ubuntu Server in production and do the following steps then it's not working.

Step 1: Mode inside the project folder in the root directory and

pm2 start index.js --name "Event Booking"

Then getting the following

 App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤ 
│ index    │ 0  │ 1.0.0   │ fork │ 29897 │ online │ 0       │ 0s     │ 0%  │ 3.7 MB   │ root │ disabled 

But the project not working. Whats the issue.

Even when I run the following as

pm2 start -r dotenv/config index.js --name 'Event Booking'

Then getting error as

error: unknown option `-r'

Any other solution to run the script with pm2

When running the pm2 process run with the --node-args as follows

pm2 start index.js --name eventbooking --node-args="-r dotenv/config"

You can pass multiple arguments with space separated, other than dotenv/config I didn't need much as I am loading everything from dotenv package but showing just for demo as follows

pm2 start index.js --name eventbooking --node-args="-r dotenv/config --production --port=1337"

Solution 2:

Alternatively, you can initialize your project with pm2 init this will create the pm2 configuration file with name ecosystem.config.js

For me for some reasons the args under app didn't work so I had to add node_args again as follows

"apps": [ "name": "eventbooking", "script": "./index.js", "node_args": ["-r dotenv/config"]

Actually, I am sticking with solution 1 for cleaner and minimal code mode.

In case if anyone interested with PM2 options then please visit the following link

http://pm2.keymetrics.io/docs/usage/quick-start/

thank you very much Andy I found some alternative. Please have a look at my solutions. I will be really glad. +1 for helping me out. – Channaveer Hakari Jun 17, 2019 at 6:08 no problem my friend, and glad you got it working. I personally prefer your solution 2, as all the pm2 parameters are nicely stored in the config js file, but it depends what other tooling you are using to manage your environments. – Andy Lorenz Jun 20, 2019 at 13:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.