当我得到以下错误:

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)


当前回答

正如@DanielImfeld指出的那样,如果你在选项中指定了“cwd”,但是给定的目录不存在,ENOENT将被抛出。

其他回答

当我试图在Debian Linux系统上从VS Code编辑器中调试node.js程序时,我得到了这个错误。我注意到同样的事情在Windows上也可以正常工作。这里之前给出的解决方案没有多大帮助,因为我没有编写任何“衍生”命令。违规代码可能是由微软编写的,并隐藏在VS code程序的引擎盖下。

接下来,我注意到node.js在Windows上被称为node,但在Debian上(可能在基于Debian的系统上,如Ubuntu),它被称为nodejs。所以我创建了一个别名,从根终端运行

ln -s /usr/bin/nodejs /usr/local/bin/node

这就解决了问题。同样或类似的过程可能也适用于其他情况,即你的node.js被称为nodejs,但你运行的程序希望它被称为node,反之亦然。

使用要求(“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);
});

简单地添加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
   .....
  }

你要改变环境选项吗?

然后看看这个答案。


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