当我得到以下错误:

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)


当前回答

在花费大量时间调试这个问题之前,大多数情况下可以通过删除node_modules并重新安装包来解决。

如何安装:

如果存在锁文件,您可以使用

yarn install --frozen-lockfile

or

npm ci

各自的。如果不是的话

yarn install

or

npm i

其他回答

你要改变环境选项吗?

然后看看这个答案。


我试图生成一个节点进程和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',
  }
});

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

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

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

cd root/test/

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

mocha test.js

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

@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'。

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

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

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

我也遇到了同样的问题,但我找到了一个简单的解决方法。 如果程序已经被用户添加到PATH(例如,正常的系统命令工作),它似乎是spawn()错误。

要解决这个问题,你可以使用which模块(npm install——save which):

// Require which and child_process
const which = require('which');
const spawn = require('child_process').spawn;
// Find npm in PATH
const npm = which.sync('npm');
// Execute
const noErrorSpawn = spawn(npm, ['install']);