PM2 and cluster mode in Node.js/TypeScript

17 Views Asked by At

I have a problem with running PM2 in cluster mode. I am running it in Docker.

root@docker-desktop /s/a/v1# node -v
v20.11.1
  • my example script:
import axios from 'axios';

async function checkIP() {
    const response = await axios.get('https://api.ipify.org?format=json');
    if (response.data.ip) {
        console.log(response.data.ip);
    } 
}

checkIP();
  • ecosystem.config.cjs
module.exports = {
  apps: [{
    script: 'ts-node ./src/index.ts', // Compiled JS file path from your TypeScript file
    instances: 'max', // Use 'max' for as many instances as CPU cores or specify a number
    exec_mode: 'cluster', // Enables cluster mode
    watch: true, // Watch for file changes in development
    env: {
      NODE_ENV: 'development',
    },
    env_production: {
      NODE_ENV: 'production',
    }
  }]
};
  • tsconfig.json
{
  "compilerOptions": {
  "moduleResolution": "node",
         "esModuleInterop": true,
         "target": "es6",
         "module": "commonjs"
  }
}
  • package.json
{
  "name": "v1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.6.8",
    "got": "^14.2.1"
  }
}

How can I fix it? I've tried everything, but it's not working. I've read the entire documentation for PM2, etc., and found nothing.

0

There are 0 best solutions below