当我得到以下错误:

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)


当前回答

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

不正确的:

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

正确的:

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

其他回答

在运行测试用例时,我也遇到了这个恼人的问题,所以我尝试了许多方法来解决它。但对我来说,有效的方法是在包含主文件的目录中运行你的测试运行器,其中包含你的nodejs衍生函数,如下所示:

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

例如,这个文件名是test.js,所以只需移动到包含它的文件夹。在我的例子中,它是这样的测试文件夹:

cd root/test/

然后从运行您的测试运行程序,在我的情况下,它是摩卡,所以它将是这样的:

mocha test.js

我浪费了一天多的时间来弄清楚。享受! !

注意:此错误几乎总是由于命令不存在、工作目录不存在或由windows特有的错误引起的。

我发现了一个特别简单的方法来了解问题的根本原因:

Error: spawn ENOENT

这个错误的问题是,在错误消息中很少有信息告诉你调用的位置,即没有找到哪个可执行/命令,特别是当你有一个很大的代码库,其中有很多衍生调用时。另一方面,如果我们知道导致错误的确切命令,那么我们可以根据@laconbass的回答来解决问题。

我发现了一种非常简单的方法来发现导致问题的命令,而不是像@laconbass的回答中建议的那样在代码中到处添加事件侦听器。关键思想是用一个包装器包装原始的衍生调用,该包装器打印发送给衍生调用的参数。

这是包装器函数,把它放在index.js或任何你的服务器启动脚本的顶部。

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        console.log('spawn called');
        console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();

然后,下次运行应用程序时,在未捕获异常的消息之前,您将看到如下内容:

spawn called
{ '0': 'hg',
  '1': [],
  '2':
   { cwd: '/* omitted */',
     env: { IP: '0.0.0.0' },
     args: [] } }

通过这种方式,你可以很容易地知道哪个命令实际上被执行了,然后你可以找出为什么nodejs不能找到可执行文件来解决这个问题。

Windows解决方案:用节点交叉生成替换生成。例如,在你的app.js的开头:

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

将C:\Windows\System32\添加到path环境变量。

步骤

进入我的电脑和属性 点击高级设置 然后是环境变量 选择Path,然后单击edit 如果不存在,则粘贴以下文件:C:\Windows\System32\ 关闭命令提示符 运行您想要运行的命令

你要改变环境选项吗?

然后看看这个答案。


我试图生成一个节点进程和TIL,您应该在生成时扩展现有的环境变量,否则您将失去PATH环境变量和其他可能重要的环境变量。

这就是我的解决方案:

const nodeProcess = spawn('node', ['--help'], {
  env: {
    // by default, spawn uses `process.env` for the value of `env`
    // you can _add_ to this behavior, by spreading `process.env`
    ...process.env,
    OTHER_ENV_VARIABLE: 'test',
  }
});