当我得到以下错误:

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});

其他回答

对我来说,我在package.json中做了以下更改。

  "version": "0.0.0",
  "scripts": {
    "dev": "vite --open",    // delete this line
    "dev": "vite",           // with this one
   .....
  }

对于Windows上的ENOENT, https://github.com/nodejs/node-v0.x-archive/issues/2318#issuecomment-249355505修复它。

如更换产卵(npm, [' v '], {stdio:“继承”}):

对于所有node.js版本: 产卵(/ ^赢/ test (process.platform) ?npm。Cmd ': 'npm', ['-v'], {stdio: 'inherit'}) 对于node.js 5。X及以后: 产卵(npm, [' v '], {stdio:“继承”,外壳:真})

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

使用要求(“child_process”)。Exec代替产卵更具体的错误消息!

例如:

var exec = require('child_process').exec;
var commandStr = 'java -jar something.jar';

exec(commandStr, function(error, stdout, stderr) {
  if(error || stderr) console.log(error || stderr);
  else console.log(stdout);
});

试了所有的都没用,我的系统有不同的问题。

我的解决方案是 运行命令: C:\Program Files\git\bin\bash.exe