events.js:72
throw er; // Unhandled \'error\' event
^
Error: spawn ENOENT
at errnoException (chil
Environment issues
PATH
environment variable.Windows-only bugs/quirks
Wrong spawn('command', ['--argument', 'list'], { cwd, env, ...opts })
usage
opts.cwd
) does not exist · see leeroy-brun's answerString
spawn('command --wrong --argument list')
spawn('ENV_VAR=WRONG command')
Array
specified as String
spawn('cmd', '--argument list')
PATH
env variable spawn('cmd', [], { env: { variable } }
spawn('cmd', [], { env: { ...process.env, variable } }
There are 2 posible origins for
ENOENT
:
- Code you are writing
- Code you depend on
When origin is code you depend on, usual cause is an Environment Issue (or windows quirk)
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!!
I ran into this problem on Windows, where calling exec
and spawn
with the exact same command (omitting arguments) worked fine for exec
(so I knew my command was on $PATH
), but spawn
would give ENOENT. Turned out that I just needed to append .exe
to the command I was using:
import { exec, spawn } from 'child_process';
// This works fine
exec('p4 changes -s submitted');
// This gives the ENOENT error
spawn('p4');
// But this resolves it
spawn('p4.exe');
// Even works with the arguments now
spawn('p4.exe', ['changes', '-s', 'submitted']);
in windows, simply adding shell: true
option solved my problem:
incorrect:
const { spawn } = require('child_process');
const child = spawn('dir');
correct:
const { spawn } = require('child_process');
const child = spawn('dir', [], {shell: true});
@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.
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');