When I 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)
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:
- using spawn function with NODE_ENV=production
- node.js child_process.spawn ENOENT error - only under supervisord
- spawn ENOENT node.js error
- https://stackoverflow.com/questions/27603713/nodejs-spawn-enoent-error-on-travis-calling-global-npm-package
- Node JS - child_process spawn('npm install') in Grunt task results in ENOENT error
- Running "foreman" task Fatal error: spawn ENOENT
- unhandled error event in node js Error: spawn ENOENT at errnoException (child_process.js:975:11)
- Node.js SpookyJS: error executing hello.js
- https://stackoverflow.com/questions/26572214/run-grunt-on-a-directory-nodewebkit
- Run exe file with Child Process NodeJS
- Node: child_process.spawn not working on Java even though it's in the path (ENOENT)
- spawn ENOENT error with NodeJS (PYTHON related)
- image resizing is not working in node.js (partial.js) (non-installed dependency)
- npm install error ENOENT (build dependency problem)
- Cannot install node.js - oracle module on Windows 7 (build dependency problem)
- Error installing gulp using nodejs on windows (strange case)
Step 1: Ensure
spawn
is called the right wayFirst, review the docs for child_process.spawn( command, args, options ):
Ensure you are not putting any command line arguments in
command
and the wholespawn
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
, orchild_process.spawn
, i.e.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.
Execute and you should get the file path and line number where your 'error' listener was registered. Something like:
If the first two lines are still
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 setThere are two possible scenarios:
spawn
behaviour, so child process environment will be the same asprocess.env
.env
object tospawn
on theoptions
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
Example for scenario 2
The absence of
PATH
(i.e., it'sundefined
) will causespawn
to emit theENOENT
error, as it will not be possible to locate anycommand
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 inPATH
Spawn may emit the
ENOENT
error if the filenamecommand
(i.e, 'some-command') does not exist in at least one of the directories defined onPATH
.Locate the exact place of
command
. On most linux distributions, this can be done from a terminal with thewhich
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
Example usage of which and its output when a command is 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 andcommand
is accessible from it, you should be able to spawn your child process withoutspawn ENOENT
being thrown.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
):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:
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:and your command should be back in business.
Add
C:\Windows\System32\
to thepath
environment variable.Steps
Go to my computer and properties
Click on Advanced settings
Then on Environment variables
Select
Path
and then click on editPaste the following if not already present:
C:\Windows\System32\
Close the command prompt
Run the command that you wanted to run
solution in my case
Ensure module to be executed is installed or full path to command if it's not a node module