2

I'm trying to use pm2 with ts-node for deployment.

When I use cluster-mode, pm2 instance error occurs that Cannot find module...

Error: Cannot find module '{path}/start'

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

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

Here is my ecosystem.config.js and "production": "pm2-runtime start ecosystem.config.js --env production" this is my package script.

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

When I use fork-mode not cluster-mode, The app works well. I don't know how to solve this problem. please tell me any ideas.

Community
  • 1
  • 1
kkangil
  • 1,616
  • 3
  • 13
  • 27

1 Answers1

1

From my understanding, using any interpreter other than JS requires fork mode thus prohibits direct execution oy typescript. You need to compile /server.ts to /server.js to enable cluster mode.

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

The easiest way to use transpilers with PM2 is to override the execution interpreter (exec_interpreter). Note that if this is changed, your code will only work in fork_mode.

Cluster and Fork mode difference in PM2

maosmurf
  • 1,858
  • 17
  • 16