简体   繁体   中英

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

After ages of looking and experimenting, it seems that it doesn't work in cluster mode, but it does in fork mode. Try running it in fork mode.

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'],
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM