当我得到以下错误:
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特有的错误引起的。
我发现了一个特别简单的方法来了解问题的根本原因:
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不能找到可执行文件来解决这个问题。
在运行测试用例时,我也遇到了这个恼人的问题,所以我尝试了许多方法来解决它。但对我来说,有效的方法是在包含主文件的目录中运行你的测试运行器,其中包含你的nodejs衍生函数,如下所示:
nodeProcess = spawn('node',params, {cwd: '../../node/', detached: true });
例如,这个文件名是test.js,所以只需移动到包含它的文件夹。在我的例子中,它是这样的测试文件夹:
cd root/test/
然后从运行您的测试运行程序,在我的情况下,它是摩卡,所以它将是这样的:
mocha test.js
我浪费了一天多的时间来弄清楚。享受! !
如何研究刷出调用引起的错误:
使用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怪癖)