首页 > 解决方案 > 如何使用 ts-node 解决集群模式下的 pm2 错误

问题描述

我正在尝试将 pm2 与 ts-node 一起使用进行部署。

当我使用cluster-mode时,出现 pm2 实例错误,即找不到模块...

错误:找不到模块“{path}/start”

在 main ({path}/node_modules/ts-node/dist/bin.js:178:20)

在对象。({path}/node_modules/ts-node/dist/bin.js:404:5)

这是我的生态系统.config.js,"production": "pm2-runtime start ecosystem.config.js --env production"这是我的包脚本。

module.exports = {
  apps: [
   {
            script: "ts-node",
            args: "./server.ts",
            instances: "max",
            exec_mode: 'cluster_mode',
            node_args: '-r esm'
            env_production: {...}
   }
  ]
}

当我使用fork-modenotcluster-mode时,该应用程序运行良好。我不知道如何解决这个问题。请告诉我任何想法。

标签: typescriptexpresspm2ts-node

解决方案


据我了解,使用 JS 以外的任何解释器都需要 fork 模式,因此禁止直接执行 oy typescript。您需要将 /server.ts 编译为 /server.js 以启用集群模式。

https://pm2.keymetrics.io/docs/tutorials/using-transpilers-with-pm2#execution-interpreter

将转译器与 PM2 一起使用的最简单方法是覆盖执行解释器 (exec_interpreter)。请注意,如果更改此设置,您的代码将仅在 fork_mode 下工作。

PM2中Cluster和Fork模式的区别


推荐阅读