cluster模式指定interpreter(自编译的node)不生效。nvm安装的pm2,fork模式单线程是成功的
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
- 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
- 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.
- 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.