当我得到以下错误:

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_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怪癖)


其他回答

虽然对某些人来说这可能是一个环境路径或其他问题,但我刚刚在Windows 10上为Visual Studio Code安装了Latex Workshop扩展,并在尝试构建/预览PDF时看到了这个错误。作为管理员运行VS Code为我解决了这个问题。

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

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

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

cd root/test/

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

mocha test.js

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

我也遇到了同样的问题,但我找到了一个简单的解决方法。 如果程序已经被用户添加到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']);

最近我也遇到了类似的问题。

Starting the development server...

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn null ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
error Command failed with exit code 1.

这是由于浏览器的.env文件中有一个错误的配置。我有BROWSER=null,但它必须是BROWSER=none。改变配置解决了我的问题。

我在Windows上遇到了这个问题,用完全相同的命令(省略参数)调用exec和spawn对于exec来说工作得很好(所以我知道我的命令在$PATH上),但spawn会给出ENOENT。原来我只需要将.exe附加到我正在使用的命令:

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']);