当我得到以下错误:

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解决方案:用节点交叉生成替换生成。例如,在你的app.js的开头:

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

其他回答

如果不是节点模块,则确保要执行的模块已安装或命令的全路径

我在windows 8上得到了同样的错误。问题是由于系统路径的环境变量丢失。将“C:\Windows\System32\”值添加到系统PATH变量中。

@laconbass的回答帮助了我,可能是最正确的。

我来这里是因为我错误地使用了spawn。 举个简单的例子:

这是不正确的:

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

这是不正确的:

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

这是正确的:

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

但是,我建议这样做:

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

这是因为cp.on('exit', fn)事件总是会触发,只要安装了bash,否则,cp.on('error', fn)事件可能会先触发,如果我们以第一种方式使用它,如果我们直接启动'npm'。

如果您在无法修改源代码的应用程序中遇到此问题,请考虑使用环境变量NODE_DEBUG设置为child_process来调用它,例如NODE_DEBUG=child_process yarn test。这将为您提供在哪个目录中调用了哪些命令行,通常最后一个细节是失败的原因。

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

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

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

const opts = {
    windowsVerbatimArguments: true
};

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

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