By laconbass

2014-12-29 12:22:00 8 Comments

When I get the following error:

        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)

What procedure can I follow to fix it?

Author note: Lots of issues with this error encouraged me to post this question for future references.

Related questions:


@Dan Alboteanu 2018-11-09 22:00:48

solution in my case

var spawn = require('child_process').spawn;

const isWindows = /^win/.test(process.platform); 

spawn(isWindows ? 'twitter-proxy.cmd' : 'twitter-proxy');
spawn(isWindows ? 'http-server.cmd' : 'http-server');

@laconbass 2019-01-30 12:51:40

While this may be a solution for win specific fixes, I don't see how it helps to debug the real cause of the ENOENT

@Alexander Mills 2016-04-12 01:32:18

@laconbass's answer helped me and is probably most correct.

I came here because I was using spawn incorrectly. As a simple example:

this is incorrect:

const s = cp.spawn('npm install -D suman', [], {
    cwd: root

this is incorrect:

const s = cp.spawn('npm', ['install -D suman'], {
    cwd: root

this is correct:

const s = cp.spawn('npm', ['install','-D','suman'], {
    cwd: root

however, I recommend doing it this way:

const s = cp.spawn('bash');
s.stdin.end(`cd "${root}" && npm install -D suman`);
s.once('exit', code => {
   // exit

this is because then the cp.on('exit', fn) event will always fire, as long as bash is installed, otherwise, the cp.on('error', fn) event might fire first, if we use it the first way, if we launch 'npm' directly.

@laconbass 2016-04-20 23:57:58

Thinking on refactoring my answer to provide a "general guide", and leaving details to each cause of the problem (miss dependencies, incorrect calls, wrong environment,...).

@bozzmob 2017-11-13 16:06:06

This is one of the most useful answer I have come across. I was doing it all wrong until I saw this answer. I was more into using the 1st wrong way of doing it. Thanks for this.

@Alexander Mills 2017-11-21 04:10:49

everyone who likes this answer, may also be interested in this native alternative:

@sonnb 2018-05-09 10:32:00

This should be accepted.

@givanse 2019-02-28 22:49:44

Downvoted because if what you want to have is a shell then you should use child_process.exec or pass shell: true to spawn.

@Alexander Mills 2019-02-28 22:53:48

@givanse not necessarily true - you may want to run zsh or bash or fsh depending on what shell you want to use, and the behavior is also different

@Rajkumar Bansal 2018-03-13 08:21:53

I was also going through this annoying problem while running my test cases, so I tried many ways to get across it. But the way works for me is to run your test runner from the directory which contains your main file which includes your nodejs spawn function something like this:

nodeProcess = spawn('node',params, {cwd: '../../node/', detached: true });

For example, this file name is test.js, so just move to the folder which contains it. In my case, it is test folder like this:

cd root/test/

then from run your test runner in my case its mocha so it will be like this:

mocha test.js

I have wasted my more than one day to figure it out. Enjoy!!

@laconbass 2014-12-29 12:22:00

Step 1: 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.

Ensure you are not putting any command line arguments in command and the whole spawn call is valid. Proceed to next step.

Step 2: Identify the Event Emitter that emits the error event

Search on your source code for each call to spawn, or child_process.spawn, i.e.

spawn('some-command', [ '--help' ]);

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

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

      throw err;
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34)

If the first two lines are still

        throw er; // Unhandled 'error' event

do this step again until they are not. You must identify the listener that emits the error before going on next step.

Step 3: Ensure the environment variable $PATH is set

There are two possible scenarios:

  1. You rely on the default spawn behaviour, so child process environment will be the same as process.env.
  2. 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']);

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 });

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.

When PATH is correctly set, proceed to next step. It should be a directory, or a list of directories. Last case is the usual.

Step 4: 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

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

> which some-command
bash: type: some-command: not found

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.

@fredrikekelund 2015-04-16 05:52:48

The problem for me was indeed that PATH was undefined. Do you know what causes this? I asked a question about it as well…

@laconbass 2015-04-17 16:02:10

I remember a time it was undefined because the way I was spawning the node proccess.

@CodeManiak 2015-07-09 22:13:48

This has been very helpful to my debugging of Spawn ENOENT. I've referenced it multiple times. Thanks!

@Alexander Mills 2015-08-26 19:28:57

if it's an ENOENT error, why would the command not be recognized on the OS? it seems reasonable that the command is recognized but something is wrong with reading the file itself...

@laconbass 2015-09-13 15:37:48

@AlexMills Usually the situation you describe is reported as EACCES

@Daniel Imfeld 2015-11-19 22:37:37

I've also found that ENOENT will be thrown if you specify cwd in the options, but the given directory does not exist.

@GreenAsJade 2015-11-29 08:37:38

@DanielImfeld TOTAL SAVIOUR. You should write an answer that says this.

@cdaringe 2016-02-19 06:27:35

I have a linux box and an osx box, both node versions reporting using v4.2.4. On the OSX machine if a try/catch wrap a call to spawn('bogus-binary', ...), it lands in the catch statement. in the linux box, it shows up in the event emitter. this is wild.

@laconbass 2016-02-20 05:49:24

@cdaringe please post a new question with the code and let's see what the problem may be

@anty 2016-08-11 11:52:30

When you are using spawn('some-command', ['--help'], { env: env }); as exemplified by Step 3 in this answer and are passing a custom environment, be sure to specify the PATH, for instance: { env: { PATH: process.env.PATH } }. The env option will not inherit variables from your current env by default.

@Tom 2018-08-05 05:40:42

I was running the command as root/ with sudo so the PATH environment variable was not the same. Step 3 was the step that pointed this out to me. Thanks!

@Nickofthyme 2019-01-24 23:20:48

I was able to solve my issue by passing shell: true to the spawn options.

@Matt Molnar 2019-03-21 15:43:06

Also, if your Node process is running inside a container like Docker, make sure the command you're trying to run has been added to the Dockerfile

@Joel B 2017-06-18 00:48:34

If you're on Windows Node.js does some funny business when handling quotes that may result in you issuing a command that you know works from the console, but does not when run in Node. For example the following should work:

spawn('ping', ['""'], {});

but fails. There's a fantastically undocumented option windowsVerbatimArguments for handling quotes/similar that seems to do the trick, just be sure to add the following to your opts object:

const opts = {
    windowsVerbatimArguments: true

and your command should be back in business.

 spawn('ping', ['""'], { windowsVerbatimArguments: true });

@laconbass 2017-06-29 08:24:29

Don't quote the arguments inside the array

@Joel B 2017-06-30 23:51:57

@laconbass This is an obviously trivial example to convey the concept and so the quotes could be removed. However, there are cases where you absolutely need to quote the arguments (for example if you need to pass an argument that has a path with a space in it: "C:\Program Files\..."). I posted it here because, even though it may not have been the cause of your specific error case, it will hopefully help someone else experiencing this cryptic error because of Node's handling of quotes on Windows like I was encountering.

@laconbass 2017-07-04 23:00:34

node.js already makes some Black Magic and silently quotes arguments "properly". Your example should work without the undocumented option you mention, by unquoting argument inside the array.

@Troncoso 2018-11-16 12:47:09

Just to add my own experience, I was running a java process from node. This error happened to me because of quotes around the command, rather than the argument. Test with spaces in the command path and it still works without quotes

@de Raad 2017-06-03 08:51:29

Use require('child_process').exec instead of spawn for a more specific error message!

for example:

var exec = require('child_process').exec;
var commandStr = 'java -jar something.jar';

exec(commandStr, function(error, stdout, stderr) {
  if(error || stderr) console.log(error || stderr);
  else console.log(stdout);

@Dalton 2016-12-25 20:50:14

Ensure module to be executed is installed or full path to command if it's not a node module

@Li Zheng 2016-09-25 02:22:47

For ENOENT on Windows, fix it.

e.g. replace spawn('npm', ['-v'], {stdio: 'inherit'}) with:

  • for all node.js version:

    spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['-v'], {stdio: 'inherit'})
  • for node.js 5.x and later:

    spawn('npm', ['-v'], {stdio: 'inherit', shell: true})

@Deilan 2017-06-05 17:36:36

Where to do these modifications?

@Ted Nyberg 2018-08-13 09:23:21

Key part is adding shell: true

@vmit dhawan 2016-08-13 17:49:38

Add C:\Windows\System32\ to the path environment variable.


  1. Go to my computer and properties

  2. Click on Advanced settings

  3. Then on Environment variables

  4. Select Path and then click on edit

  5. Paste the following if not already present: C:\Windows\System32\

  6. Close the command prompt

  7. Run the command that you wanted to run

Windows 8 Environment variables screenshot

@Emile Bergeron 2016-08-31 19:11:01

This is a duplicate of chayasan's answer

@Gum Joe 2016-05-21 19:02:33

I ran into the same problem, but I found a simple way to fix it. It appears to be spawn() errors if the program has been added to the PATH by the user (e.g. normal system commands work).

To fix this, you can use the which module (npm install --save which):

// Require which and child_process
const which = require('which');
const spawn = require('child_process').spawn;
// Find npm in PATH
const npm = which.sync('npm');
// Execute
const noErrorSpawn = spawn(npm, ['install']);

@jaunt 2019-04-05 12:55:46

Thank you! This is the only thing that worked for me

@Leeroy Brun 2016-06-17 14:44:33

As @DanielImfeld pointed it, ENOENT will be thrown if you specify "cwd" in the options, but the given directory does not exist.

@maximedupre 2016-08-16 17:55:07

Thanks! This solved my problem.

@Mitro 2017-10-03 09:41:15

so is there a way to execute in a specific directory the command?

@leocreatini 2018-03-01 00:24:33

thanks! Spent 30 minutes trying to debug this, was just a simple issue to fix

@Museful 2019-01-15 20:56:28

In Windows (7) it seems you also need to include the drive letter in the cwd path: 'c:/...' and not just '/...'

@chayasan 2016-05-06 11:48:08

I got the same error for windows 8.The issue is because of an environment variable of your system path is missing . Add "C:\Windows\System32\" value to your system PATH variable.

@MTGradwell 2016-02-28 00:20:10

I was getting this error when trying to debug a node.js program from within VS Code editor on a Debian Linux system. I noticed the same thing worked OK on Windows. The solutions previously given here weren't much help because I hadn't written any "spawn" commands. The offending code was presumably written by Microsoft and hidden under the hood of the VS Code program.

Next I noticed that node.js is called node on Windows but on Debian (and presumably on Debian-based systems such as Ubuntu) it's called nodejs. So I created an alias - from a root terminal, I ran

ln -s /usr/bin/nodejs /usr/local/bin/node

and this solved the problem. The same or a similar procedure will presumably work in other cases where your node.js is called nodejs but you're running a program which expects it to be called node, or vice-versa.

@Nilzor 2016-02-22 19:16:46

Windows solution: Replace spawn with node-cross-spawn. For instance like this at the beginning of your app.js:

(function() {
    var childProcess = require("child_process");
    childProcess.spawn = require('cross-spawn');

@Alfred UC 2016-03-23 08:25:44

Thanks! It works for me.

@Bogdan Trusca 2016-08-19 12:47:02

worked except it is a drop-in, no need for child_process. Exactly the same way as node's spawn or spawnSync, so it's a drop in replacement. var spawn = require('cross-spawn'); // Spawn NPM asynchronously var child = spawn('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });

@Anonymous 2017-08-23 14:50:57

Life saving comment :) This is the only way I could execute "ng build" from gulp

@ERJAN 2018-09-23 10:48:24

@Nilzor, can u take a look at my problem? i cant' run create-react-app , get this similar error…

@Alex Turpin 2015-08-05 19:37:47

For anyone who might stumble upon this, if all the other answers do not help and you are on Windows, know that there is currently a big issue with spawn on Windows and the PATHEXT environment variable that can cause certain calls to spawn to not work depending on how the target command is installed.

@Nilzor 2016-02-22 18:46:15

And what's the solution?

@Nilzor 2016-02-22 19:17:15

Using node-cross-spawn worked for me. See answer below:

@reduckted 2016-08-16 05:29:51

Spent ages trying to find what was wrong and this ended up being the problem. I gave up on spawn and just used exec instead.

@Jiaji Zhou 2015-01-11 02:40:30

I found a particular easy way to get the idea of the root cause of:

Error: spawn ENOENT

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. On the other hand, if we know the exact command that cause the error then we can follow @laconbass' answer to fix the problem.

I found a very easy way to spot which command cause the problem rather than adding event listeners everywhere in your code as suggested in @laconbass' answer. 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');
        var result = oldSpawn.apply(this, arguments);
        return result;
    childProcess.spawn = mySpawn;

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': [],
   { cwd: '/* omitted */',
     env: { IP: '' },
     args: [] } }

In this way you can easily know which command actually is executed and then you can find out why nodejs cannot find the executable to fix the problem.

@laconbass 2015-01-16 15:11:19

I'm accepting your answer because it's awesome simplicity to deal with the problem :D

@Adam Monsen 2015-04-16 05:08:32

Here's another idea: just change spawn() to exec() and try again. exec() will tell you what command it tried to run.

@Dan Nissenbaum 2015-06-21 00:47:46

Important: Make sure to place the code above as close to the start of the main JS file as possible. If you load other modules first, they can stash away the 'spawn' function and the override here will never be called.

@newguy 2016-11-03 08:28:04

I have no luck using the script. It doesn't work at all.

@Felix Eve 2017-02-07 04:04:12

So how would you use this method in a grunt file? I'm not sure where to put this.

@Yann Duran 2017-03-03 07:12:04

This worked perfectly for me. I just put this at the top of my gulpfile.js file, and bingo bango bongo, spawn logging!

@agpt 2018-10-04 13:19:44

This is called as monkey patching ! :)

@demisx 2018-12-14 03:16:57

Hmm... Didn't work for me. Placed it at the top of gulpfile.js

@Darius 2019-02-07 11:25:32

Replace console.log(arguments) with console.log(JSON.stringify(arguments, null, 4)) in case of nested (non-circular) objects that you want to print

@PromInc 2015-06-12 20:01:19

In my case, I was getting this error thrown due to the necessary dependent system resources not being installed.

More specifically, I have a NodeJS app that is utilizing ImageMagick. Despite having the npm package installed, the core Linux ImageMagick was not installed. I did an apt-get to install ImageMagick and after that all worked great!

@Somename 2017-04-04 05:23:50

Does windows need ImageMagick installed as well? Im testing on windows and getting error

Related Questions

Sponsored Content

29 Answered Questions

18 Answered Questions

[SOLVED] How to exit in Node.js

  • 2011-03-10 21:30:05
  • Bryan Field
  • 758761 View
  • 1614 Score
  • 18 Answer
  • Tags:   node.js

35 Answered Questions

[SOLVED] How do I debug Node.js applications?

3 Answered Questions

[SOLVED] How do I get started with Node.js

  • 2010-03-01 04:09:28
  • Joneph O.
  • 1215676 View
  • 1265 Score
  • 3 Answer
  • Tags:   javascript node.js

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

11 Answered Questions

[SOLVED] What is the purpose of Node.js module.exports and how do you use it?

  • 2011-03-15 11:56:10
  • mrwooster
  • 447514 View
  • 1347 Score
  • 11 Answer
  • Tags:   javascript node.js

3 Answered Questions

[SOLVED] react native windows, spawn npm ENOENT error

7 Answered Questions

[SOLVED] Node.js/Windows error: ENOENT, stat 'C:\Users\RT\AppData\Roaming\npm'

0 Answered Questions

image resizing is not working in node.js (partial.js)

  • 2014-01-25 04:40:22
  • Sudhir Vadodariya
  • 242 View
  • 0 Score
  • 0 Answer
  • Tags:   node.js

1 Answered Questions

youtube-dl nodejs has issues Error: spawn EACCES

Sponsored Content