Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 Cluster Mode - Cannot find module 'dotenv/config'

I am trying to run multiple apps using PM2 in cluster mode with config file given below:

    "apps": [
        {
            "name": "Node APIs",
            "script": "./server",
            "watch": true,
            "node_args": "-r dotenv/config",
            "instances": "max",
            "exec_mode": "cluster"
        },
        {
            "name": "Node Batch",
            "script": "./batch_process",
            "watch": true,
            "node_args": "-r dotenv/config"
        }
    ]
}

Node APIs process is getting errored in pm2 list while Node Batch Process works fine. When I check ~/.pm2/pm2.logs it says:

Cannot find module 'dotenv/config'

I have installed dotenv module both locally and globally but still showing same error.

Also PM2 cluster mode works fine in my local machine but on AWS EC2 it shows above error. What am I missing?

PM2: v4.4.0 NodeJS: v8.12.0

like image 290
idrisjafer Avatar asked Oct 25 '25 10:10

idrisjafer


1 Answers

Try specifying the full path to your package in node_modules via the node_args parameter, even if you are already specifying it in cwd.

It will work in cluster mode.

   {
      name: 'app-api',
      script: '/full/path/to/app/api.js',
      instances: 2,
      exec_mode: 'cluster',
      cwd: '/full/path/to/app',
      node_args: ['-r', '/full/path/to/app/node_modules/dotenv/config'],
    }
like image 56
Ben Taber Avatar answered Oct 28 '25 00:10

Ben Taber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!