[Solved-5 Solutions] Error spawn enoent on node.js



Error Description:

We get the following error:

events.js:72
throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34) 
click below button to copy the code. By - nodejs tutorial - team

Solution 1:

Error: spawn ENOENT 
click below button to copy the code. By - nodejs tutorial - team

The problem of this error is, there is really little information in the error message to tell you where the call site is, i.e. which executable/command is not found, especially when you have a large code base where there are a lot of spawn calls.

  • The key idea is to wrap the original spawn call with a wrapper which prints the arguments send to the spawn call.
  • Here is the wrapper function, put it at the top of the index.js or whatever your server's starting script.
(function(){
var childProcess = require("child_process");
var oldSpawn = childProcess.spawn;
function mySpawn(){
        console.log('spawn called');
        console.log(arguments);
var result = oldSpawn.apply(this, arguments);
return result;
}
    childProcess.spawn = mySpawn;
})(); 
click below button to copy the code. By - nodejs tutorial - team

Then the next time you run your application, before the uncaught exception's message you will see something like that:

spawn called
{'0':'hg',
'1':[],
'2':
{ cwd:'/* omitted */',
     env:{ IP:'0.0.0.0'},
     args:[]}} 
click below button to copy the code. By - nodejs tutorial - team

Solution 2:

Ensure spawn is called the right way

First, review the docs for child_process.spawn( command, args, options ):

"Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array. The third argument is used to specify additional options, which defaults to: { cwd: undefined, env: process.env } Use env to specify environment variables that will be visible to the new process, the default is process.env. "

Solution 3:

Identify the Event Emitter that emits the error event

  • Search on your source code for each call to spawn, or child_process.spawn
spawn('some-command',['--help']);
 
click below button to copy the code. By - nodejs tutorial - team

and attach there an event listener for the 'error' event, so you get noticed the exact Event Emitter that is throwing it as 'Unhandled'. After debugging, that handler can be removed.

spawn('some-command',['--help'])
.on('error',function( err ){throw err })
; 
click below button to copy the code. By - nodejs tutorial - team

Execute and you should get the file path and line number where your 'error' listener was registered. Something like:

/file/that/registers/the/error/listener.js:29
      throw err;
            ^
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34) 
click below button to copy the code. By - nodejs tutorial - team
events.js:72
        throw er; // Unhandled 'error' event 
click below button to copy the code. By - nodejs tutorial - team

Solution 4:

Ensure the environment variable $PATH is set

There are two possible scenarios:

  • You rely on the default spawn behaviour, so child process environment will be the same as process.env.
  • You are explicity passing an env object to spawn on the options argument.

In both scenarios, you must inspect the PATH key on the environment object that the spawned child process will use.

Example for scenario 1

// inspect the PATH key on process.env
console.log( process.env.PATH );
spawn('some-command',['--help']); 
click below button to copy the code. By - nodejs tutorial - team

Example for scenario 2

var env = getEnvKeyValuePairsSomeHow();
// inspect the PATH key on the env object
console.log( env.PATH );
spawn('some-command',['--help'],{ env: env }); 
click below button to copy the code. By - nodejs tutorial - team

The absence of PATH (i.e., it's undefined) will cause spawn to emit the ENOENT error, as it will not be possible to locate any command unless it's an absolute path to the executable file.

Solution 5:

Ensure command exists on a directory of those defined in PATH

Spawn may emit the ENOENT error if the filename command (i.e, 'some-command') does not exist in at least one of the directories defined on PATH.

Locate the exact place of command. On most linux distributions, this can be done from a terminal with the which command. It will tell you the absolute path to the executable file (like above), or tell if it's not found.

Example usage of which and its output when a command is found

> which some-command
some-command is /usr/bin/some-command 
click below button to copy the code. By - nodejs tutorial - team

Example usage of which and its output when a command is not found

> which some-command
bash: type: some-command: not found 
click below button to copy the code. By - nodejs tutorial - team
  • miss-installed programs are the most common cause for a not found command. Refer to each command documentation if needed and install it.
  • When command is a simple script file ensure it's accessible from a directory on the PATH. If it's not, either move it to one or make a link to it.
  • Once you determine PATH is correctly set and command is accessible from it, you should be able to spawn your child process without spawn ENOENT being thrown.

Related Searches to Error spawn enoent on node.js