Unitech / Unitech/pm2

PM2 and cluster mode in Node.js/TypeScript

Open
#5,790 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

I have a problem with running PM2 in cluster mode.
I am running it in Docker. How can I fix it?

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"
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The reported setup is in ecosystem.config.cjs, tsconfig.json, package.json, and src/index.ts; reproduce the Docker run with Node.js v20.11.1 and the shown PM2 cluster settings. Compare the observed behavior with PM2's cluster-mode and Docker entry points, and consider the issue done only when a concrete correction for this configuration is reproducible.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, node.js, typescript
Domain
cli, devops, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.