简体   繁体   English

PM2 集群模式 - 找不到模块 'dotenv/config'

[英]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:我正在尝试在集群模式下使用 PM2 运行多个应用程序,配置文件如下:

    "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. Node APIs 进程在pm2 list errored ,而 Node Batch Process 工作正常。 When I check ~/.pm2/pm2.logs it says:当我检查~/.pm2/pm2.logs它说:

Cannot find module 'dotenv/config'

I have installed dotenv module both locally and globally but still showing same error.我已经在本地和全局安装了dotenv模块,但仍然显示相同的错误。

Also PM2 cluster mode works fine in my local machine but on AWS EC2 it shows above error. PM2 集群模式在我的本地机器上也可以正常工作,但在 AWS EC2 上它显示上述错误。 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.尝试在 fork 模式下运行它。

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.尝试通过 node_args 参数在 node_modules 中指定 package 的完整路径,即使您已经在 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'],
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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