Skip to content

cluster模式指定interpreter(自编译的node)不生效。nvm安装的pm2,fork模式单线程是成功的 #5972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bnmgh1 opened this issue Apr 7, 2025 · 1 comment

Comments

@bnmgh1
Copy link

bnmgh1 commented Apr 7, 2025

No description provided.

@rohankamble103
Copy link

Problem Explained

In cluster mode, PM2 spawns multiple workers, and it needs the interpreter path (node) to be reliable for every forked child. When you:

Use NVM, the node path is managed inside your user shell session.

Use self-compiled Node, that binary may not be accessible outside the shell or from pm2-runtime (which runs as a different subprocess or in Docker).

So in cluster mode, PM2 fails to find the right node binary — while fork mode just works because it reuses the existing shell/node context.

Solution

  1. Explicitly define the Node interpreter path in your ecosystem.config.js

Use the full path to your node binary (e.g., /home/youruser/.nvm/versions/node/vXX.X.X/bin/node or /usr/local/bin/node):

module.exports = {
  apps: [
    {
      name: "app",
      script: "your-entry-file.js",
      exec_mode: "cluster",
      instances: "max",
      interpreter: "/full/path/to/node",
    },
  ]
}

You can get the full path by running:

which node
  1. Ensure the environment is available to PM2If you're running pm2 in a Docker container, via pm2-runtime, or as a systemd service, make sure the environment variables and $PATH are properly set.

If using NVM, add this at the top of your Dockerfile or shell profile:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 19

Or just point to the Node path directly.

  1. Try Node from NVM in Docker/PM2 RuntimeIf you're using NVM inside a Docker container, it's often easier to just install Node using curl or system binaries inside the container to avoid NVM quirks in non-interactive shells.

For example in Docker:

RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash - \
 && apt-get install -y nodejs

Optional Debug Tip
Add this inside your ecosystem.config.js:

interpreter_args: "--trace-warnings",

To get more logs if cluster mode fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants