Pm2: Error: spawn ENOENT on Ubuntu 14.04

Created on 13 Sep 2014  路  16Comments  路  Source: Unitech/pm2

Hi,

I'm getting the following error on Ubuntu 14.04 for almost anything I try to do with pm2:

Starting PM2 daemon...

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:988:11)
    at Process.ChildProcess._handle.onexit (child_process.js:779:34)

I'm getting one line per attempt in pm2.log

pavel@pavel-laptop:~/tmp/pm2$ cat ~/.pm2/pm2.log 
execvp(): No such file or directory

This is how it looks when I try it from source after a git clone:

pavel@pavel-laptop:~/tmp/pm2$ npm test

> [email protected] test /home/pavel/tmp/pm2
> NODE_ENV=test bash test/index.sh && NODE_ENV=test bash test/main.sh

Starting PM2 daemon...

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:988:11)
    at Process.ChildProcess._handle.onexit (child_process.js:779:34)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

npm ERR! not ok code 0

And this is when I try pm2 installed via npm:

pavel@pavel-laptop:~/tmp/pm2$ DEBUG=* PM2_DEBUG=true node_modules/pm2/bin/pm2 -v
  pm2:satan [PING PM2] Trying to connect to server +0ms
  axon:sock connect attempt 0.0.0.0:6666 +2ms
  axon:sock error ECONNREFUSED +3ms
  axon:sock ignored ECONNREFUSED +0ms
  axon:sock attempting reconnect +100ms
  axon:sock closing +1ms
  axon:sock closing 0 connections +0ms
  pm2:satan Daemon not launched +0ms
  axon:sock connect attempt 0.0.0.0:6666 +1ms
  pm2:satan PM2 alive: false +0ms
Starting PM2 daemon...
  pm2:satan Launching daemon +1ms

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:988:11)
    at Process.ChildProcess._handle.onexit (child_process.js:779:34)

Most helpful comment

@Unitech I think I have found the cause of this bug which I just experienced:

How to reproduce: you have two version of nodejs on your computer. One is an old version installed in /usr/bin/node and the other one is a recent version located in 聽./nodejs/node. I launch my pm2 manager programmatically, by calling ./nodejs/node myManager.js, which will call pm2.connect(), which will try to spawn the PM2 Daemon.

What happen is that the PM2 Daemon will be launched using the old node binary from /usr/bin/node instead of the recent node binary running my myManager.js script (locatedin ./nodejs/node).

The erroneous code is in Satan.launchDaemon:

var child = require('child_process').spawn('node', node_args, {
    detached   : true,
    cwd        : process.cwd(),
    env        : util._extend({
      'SILENT' : cst.DEBUG ? !cst.DEBUG : true,
      'HOME'   : (process.env.PM2_HOME || process.env.HOME || process.env.HOMEPATH)
    }, process.env),
    stdio      : ['ipc', out, err]
  });

What this does is look for a node binary _in the current directory_, and then default to node binaries _in the path_. _BUT_ the node binary running the current code is in ./nodejs/node. To launch the PM2 daemon _with the same node binary as the current process_, you need to use process.execPath (see docs):

var child = require('child_process').spawn(process.execPath, node_args, {
    detached   : true,
    cwd        : process.cwd(),
    env        : util._extend({
      'SILENT' : cst.DEBUG ? !cst.DEBUG : true,
      'HOME'   : (process.env.PM2_HOME || process.env.HOME || process.env.HOMEPATH)
    }, process.env),
    stdio      : ['ipc', out, err]
  });

TL;DR: in Satan.js, the code spawning the PM2 Daemon should use the same node binary as the the current process, so use process.execPath.

duplicate of https://github.com/Unitech/PM2/issues/756

All 16 comments

I'm getting exactly the same error here, also Ubuntu 14.04.

node version is v0.10.25

455

Did you update pm2 recently?

npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian (did you read this?)

I read it, I don't have a binary named "node" on my system.
This is the latest pm2 (from npm install pm2) and node version 0.10.25

I've found adding a symlink fixes this for manual start:

sudo ln -swhich nodejs/usr/local/bin/node

Not for reboot though

you should not install the legacy nodejs package, but with nvm instead

Unitech - I could not get this to work with nvm. Despite your warnings following this answer: http://stackoverflow.com/questions/21168141/can-not-install-packages-using-node-package-manager-in-ubuntu and installing:

sudo apt-get install nodejs-legacy

Automatically creates the symlink and reboot now starts pm2 and loads dumps. I suspect doing a manual install means its easy to miss some config.

1) Fully remove your packages installations - aptitude remove nodejs-legacy nodejs node
2) Purge and clean
3) curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | bash (https://github.com/creationix/nvm) - curl required of use the wget one-liner
4) source ~/.bash_profile or source ~/.nvm/nvm.sh
5) nvm install 0.12
6) node binary is loacted there: ~/.nvm/versions/nodejs/v0.12/bin/node

Soyuka answer is the way to solve this if you experience this bug

I have also these error.. I want to release an apk for my app but this error occur.. I don't have an idea on how to fix it.

@jgamao Have you tried soyuka's answer?

@jorge-d, Yes I have tried it but there's another error occurred.

which is?

@Unitech I think I have found the cause of this bug which I just experienced:

How to reproduce: you have two version of nodejs on your computer. One is an old version installed in /usr/bin/node and the other one is a recent version located in 聽./nodejs/node. I launch my pm2 manager programmatically, by calling ./nodejs/node myManager.js, which will call pm2.connect(), which will try to spawn the PM2 Daemon.

What happen is that the PM2 Daemon will be launched using the old node binary from /usr/bin/node instead of the recent node binary running my myManager.js script (locatedin ./nodejs/node).

The erroneous code is in Satan.launchDaemon:

var child = require('child_process').spawn('node', node_args, {
    detached   : true,
    cwd        : process.cwd(),
    env        : util._extend({
      'SILENT' : cst.DEBUG ? !cst.DEBUG : true,
      'HOME'   : (process.env.PM2_HOME || process.env.HOME || process.env.HOMEPATH)
    }, process.env),
    stdio      : ['ipc', out, err]
  });

What this does is look for a node binary _in the current directory_, and then default to node binaries _in the path_. _BUT_ the node binary running the current code is in ./nodejs/node. To launch the PM2 daemon _with the same node binary as the current process_, you need to use process.execPath (see docs):

var child = require('child_process').spawn(process.execPath, node_args, {
    detached   : true,
    cwd        : process.cwd(),
    env        : util._extend({
      'SILENT' : cst.DEBUG ? !cst.DEBUG : true,
      'HOME'   : (process.env.PM2_HOME || process.env.HOME || process.env.HOMEPATH)
    }, process.env),
    stdio      : ['ipc', out, err]
  });

TL;DR: in Satan.js, the code spawning the PM2 Daemon should use the same node binary as the the current process, so use process.execPath.

duplicate of https://github.com/Unitech/PM2/issues/756

@jshkurti thanks for the quick fix :+1:

@davidrapin I had the same problem . Your analysis is very good. thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shaunwarman picture shaunwarman  路  3Comments

xzycn picture xzycn  路  3Comments

mario-mui picture mario-mui  路  3Comments

rajendar38 picture rajendar38  路  3Comments

lefam picture lefam  路  3Comments