【问题标题】:Unable to create node script cluster with PM2无法使用 PM2 创建节点脚本集群
【发布时间】:2020-09-06 13:06:45
【问题描述】:

我正在尝试使用 PM2 创建节点脚本集群,但出现错误且无法正常工作

打字稿中的主节点脚本

import express from 'express';
import mongoose from 'mongoose';

const app = express();

app.get('/', (req, res) => {
    const tickets = {};
    res.send(tickets);
});

const setup = async () => {
    console.clear();

    try {
        await mongoose.connect('mongodb://127.0.0.1:27017/tickets', {
            useNewUrlParser: true, 
            useUnifiedTopology: true,
            useCreateIndex: true
        });
    } catch(err) {
        console.log(err);
    } 

    app.listen(5001, () => {
        console.log('listing app on 5001');
    });
}

setup();

NPM 运行脚本

ts-node-dev --poll index.ts

我的 PM2 启动脚本 process.json

{
    "apps" : [
        {
            "name"       : "main-server",
            "script"     : "npm start",
            "autorestart": true,
            "instances"  : 4,
            "exec_mode"  : "cluster"            
        }
    ]
}

并得到错误

SyntaxError: Invalid or unexpected token
3|main-ser |     at wrapSafe (internal/modules/cjs/loader.js:1047:16)
3|main-ser |     at Module._compile (internal/modules/cjs/loader.js:1097:27)
3|main-ser |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
3|main-ser |     at Module.load (internal/modules/cjs/loader.js:977:32)
3|main-ser |     at Function.Module._load (internal/modules/cjs/loader.js:877:14)
3|main-ser |     at /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js:303:25
3|main-ser |     at wrapper (/usr/local/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16)
3|main-ser |     at next (/usr/local/lib/node_modules/pm2/node_modules/async/waterfall.js:96:20)
3|main-ser |     at /usr/local/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
3|main-ser |     at WriteStream.<anonymous> (/usr/local/lib/node_modules/pm2/lib/Utility.js:186:13)

当我使用单个实例“ts-node-dev --poll index.ts”运行直接命令时,它在一次实例上运行良好。但在 PM2 集群模式下,它无法工作并且应用程序无法加载。

【问题讨论】:

  • 使用 pm2 运行的命令看起来如何?
  • pm2 start process.json [process.json command already added in question]

标签: node.js typescript pm2 node-cluster


【解决方案1】:

我认为问题在于您正在尝试使用 PM2 运行打字稿代码。我自己从未尝试过,但显然 PM2 有一个TS Plugin。如果这不起作用,您总是可以自己编译代码,然后通过 PM2 运行它

【讨论】:

    【解决方案2】:

    我认为您必须以不同的方式调用主脚本文件。不久之前,我试图让一个进程文件运行。在花了很多时间之后,它以某种方式对我有用。试试这个配置:

    {
        "apps" : [
            {
                "name": "main-server",
                "script": "./index.ts",
                "node_args": [
                    "ts-node-dev",
                    "--poll"
                ],
                "autorestart": true,
                "instances": 4,
                "exec_interpreter": "node",
                "exec_mode": "cluster",
                "env": {
                    "NODE_ENV": "development"
                },
                "env_production": {
                    "NODE_ENV": "production"
                }
            }
        ]
    }
    
    • script 调用的是文件,而不是 npm 命令
    • 参数在node_args中给出
    • exec_interpreternode 或整个路径,例如/usr/bin/nodejs
    • 不确定,但在任何地方阅读以定义 env 很重要。

    使用pm2 start process.json 在开发模式下运行它 使用pm2 start process.json --env production 的 prod 模式。

    未测试。祝你好运。

    【讨论】:

    • 是的,它的工作原理。我已将 exec_interpreter 直接更改为 ts-code
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多