如何使用ts-node解决群集模式下的pm2错误

时间:2019-12-30 01:10:18

标签: typescript express pm2 ts-node

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

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

  

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

     

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

     

at Object。 ({path} /node_modules/ts-node/dist/bin.js:404:5)

这是我的ecosystem.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-mode而不是cluster-mode时,该应用程序运行良好。 我不知道如何解决这个问题。 请告诉我任何想法。

1 个答案:

答案 0 :(得分:0)

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

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

在PM2中使用转译器的最简单方法是覆盖 执行解释器(exec_interpreter)。请注意,如果这是 更改后,您的代码将只能在fork_mode下工作。

Cluster and Fork mode difference in PM2