当我得到以下错误:

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)

我该采取什么程序来修理呢?

作者注:这个错误的许多问题鼓励我发布这个问题,以供将来参考。

相关问题:

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)


当前回答

如果你在Windows上,Node.js在处理引号时做了一些有趣的事情,可能会导致你发出一个命令,你知道从控制台工作,但不是在Node中运行。例如,以下应该工作:

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

但失败。有一个神奇的无文档选项windowsVerbatimArguments用于处理引号/类似的东西,似乎可以做到这一点,只要确保在你的opts对象中添加以下内容:

const opts = {
    windowsVerbatimArguments: true
};

你的指挥部应该能恢复正常工作了。

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

其他回答

简单地添加shell: true选项解决了我的问题:

不正确的:

const { spawn } = require('child_process');
const child = spawn('dir');

正确的:

const { spawn } = require('child_process');
const child = spawn('dir', [], {shell: true});

如何研究刷出调用引起的错误:

使用NODE_DEBUG=child_process, Credits to @karl-richter。简单,快速,2019年10月 使用包装器来装饰child_process。刷,积分到@jiaji-zhou。简单,快速,2015年1月 漫长的过程,归功于@laconbass。复杂,时间成本,2014年12月

已知的常见原因

Environment issues The command executable does not exist within the system (dependency not being installed). see prominc's answer The command executable does not exist within a directory of those specified by PATH environment variable. The executable binary was compiled with uncompatible libraries. see danilo-ramirez answer Windows-only bugs/quirks '.cmd' extension / shell: true. see li-zheng answer Administrator permisions. see steve's answer Wrong spawn('command', ['--argument', 'list'], { cwd, env, ...opts }) usage Specified working directory (opts.cwd) does not exist · see leeroy-brun's answer Argument list within command String spawn('command --wrong --argument list') Env vars within command string spawn('ENV_VAR=WRONG command') Argument list Array specified as String spawn('cmd', '--argument list') Unset PATH env variable spawn('cmd', [], { env: { variable } } => spawn('cmd', [], { env: { ...process.env, variable } }


ENOENT有两种可能的起源: 您正在编写的代码 您依赖的代码 当源代码是你依赖的代码时,通常的原因是环境问题(或windows怪癖)


使用要求(“child_process”)。Exec代替产卵更具体的错误消息!

例如:

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

如果你在Windows上,Node.js在处理引号时做了一些有趣的事情,可能会导致你发出一个命令,你知道从控制台工作,但不是在Node中运行。例如,以下应该工作:

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

但失败。有一个神奇的无文档选项windowsVerbatimArguments用于处理引号/类似的东西,似乎可以做到这一点,只要确保在你的opts对象中添加以下内容:

const opts = {
    windowsVerbatimArguments: true
};

你的指挥部应该能恢复正常工作了。

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

我发现一个案例不在这个列表中,但它值得添加:

在Alpine Linux上,如果可执行文件不兼容,Node将会出现ENOENT错误。

Alpine期望使用libc生成二进制文件。使用glibc编译的可执行文件(例如chrome作为chromium的一部分)作为系统调用的包装器,在spawn调用ENOENT时将失败。