当我得到以下错误:

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

其他回答

在我的例子中,由于没有安装必要的依赖系统资源,我抛出了这个错误。

更具体地说,我有一个使用ImageMagick的NodeJS应用程序。尽管安装了npm包,但核心Linux ImageMagick没有安装。我做了一个apt-get来安装ImageMagick,之后一切都很好!

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

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

模拟器上的本地开发

确保将包安装在本地。通过改变产卵命令与exec,我得到了一个更详细的错误,并发现我没有安装包。简单地运行,检查包是否存在:

酿造安装imagemagick

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