Search code examples
node.jsprocesspm2

Issue with PM2 and DotEnv not working on ubuntu server


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
-----------------
.env
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


Solution

  • There are 2 ways to achieve a solution.

    Solution 1:

    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/