PM2 API 连接不上

PM2 API doesn't connect

我无法通过 API 连接到 PM2 实例。

代码如下

var pm2 = require('pm2');

console.log('pm2  ', pm2);

pm2.connect(function (err) {
    console.log('pm2 error ', err);
    if (err) {
        console.error(err);
        process.exit(2);
    }
    else {
        pm2.list(function (err, res) { });
        var pm2 = require('pm2');

    }
});

我得到的错误是

$ sudo node maintanance/manage-pm2.js 
pm2   undefined
(node:56046) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
pm2 error  null
/home/ubuntu/laks/middleware-2.0/maintanance/manage-pm2.js:12
        pm2.list(function (err, res) { });
            ^

TypeError: Cannot read property 'list' of undefined
    at /home/ubuntu/laks/middleware-2.0/maintanance/manage-pm2.js:12:13
    at /home/ubuntu/laks/middleware-2.0/node_modules/pm2/lib/API.js:182:16
    at /home/ubuntu/laks/middleware-2.0/node_modules/pm2/lib/Client.js:54:16
    at Timeout._onTimeout (/home/ubuntu/laks/middleware-2.0/node_modules/pm2/lib/Client.js:371:9)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

是的,当然,我已经安装了“pm​​2”版本^4.5.4

删除此行 var pm2 = require('pm2'); 你正在重新初始化 pm2 而 pm2.list 仍然是 运行

processList 将为您提供 pm2 实例列表

var pm2 = require('pm2');

console.log('pm2  ', pm2);

pm2.connect(function (err) {
    console.log('pm2 error ', err);
    if (err) {
        console.error(err);
        process.exit(2);
    }
    else {
        pm2.list(function (err, processList) { 
            console.log(processList);
        });
    }
});