node.js 错误:在节点中使用 GM 时产生 ENOENT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16222116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: spawn ENOENT while using GM in node
提问by Olivier_s_j
When I try to resize an image like this:
当我尝试像这样调整图像大小时:
gm('public/uploads/1710410635.jpg')
.resize(240, 240)
.noProfile()
.write('public/uploads/1710410635_t.jpg', function (err) {
if (!err) console.log('done');
});
I get this error:
我收到此错误:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:945:11)
at Process.ChildProcess._handle.onexit (child_process.js:736:34)
My file structure is as follows:
我的文件结构如下:


The code is executed in the postnewsitem.jsfile
代码在postnewsitem.js文件中执行
why is this error occurring & how do I solve it ?
为什么会发生此错误以及如何解决?
edit: GraphicsMagick works, proof:
编辑:GraphicsMagick 作品,证明:


回答by Qu?nh Lê
Install ImageMagick and use subClass imageMagick.
安装 ImageMagick 并使用子类 imageMagick。
Install ImageMagick
sudo apt-get install imagemagickusing subClass imagemagick:
var gm = require('gm').subClass({ imageMagick: true });
安装 ImageMagick
sudo apt-get install imagemagick使用子类 imagemagick:
var gm = require('gm').subClass({ imageMagick: true });
回答by Matiss
I'm running nodejs on windows 7 with installed gm and imagemagick and seems that there was conflict between both modules so i googled a bit and found out how to avoit that. I added this line and that solved my ENOENT problem:
var imageMagick = gm.subClass({ imageMagick: true });so the code now looks like this:
我在安装了 gm 和 imagemagick 的 Windows 7 上运行 nodejs,似乎两个模块之间存在冲突,所以我用谷歌搜索了一下,发现了如何避免这种情况。我添加了这一行并解决了我的 ENOENT 问题:
var imageMagick = gm.subClass({ imageMagick: true });所以代码现在看起来像这样:
var gm = require('gm');
var imageMagick = gm.subClass({ imageMagick: true });
imageMagick('test/pig.jpg').rotate('green', 45).write('test/crazy_pig.jpg', function (err) {
if (!err) console.log('crazy pig has arrived');
else console.log(err);
})
OR you can do that when requiring gm, like so:
或者你可以在需要 gm 时这样做,如下所示:
var gm = require('gm').subClass({ imageMagick: true });
回答by user2620800
Had the same problem with Node.js application running on Windows using IIS. Problem has gone when I set "Load User Profile" option in "Advanced settings" of appropriate AppPool to "True"
使用 IIS 在 Windows 上运行的 Node.js 应用程序也有同样的问题。当我将相应 AppPool 的“高级设置”中的“加载用户配置文件”选项设置为“ True”时,问题就消失了
回答by Surya Purohit
I faced the same problem and solved it in the given way.
我遇到了同样的问题并以给定的方式解决了它。
var gm = require('gm');
gm('public/uploads/1710410635.jpg').options({imageMagick: true}).resize(240,240).write('public/uploads/1710410635.jpg', function (err) {
if (!err) console.log('Done');
else console.log(err);
})
Note:If you haven't installed imageMagick. Please install that first
注意:如果您还没有安装 imageMagick。请先安装
回答by epeleg
Another scenario where this might happen (when using windows) is if you try to run your code from a UNC Path. mapping a drive letter and running over the mapped drive letter solves this problem as well.
另一种可能发生这种情况的情况(使用 Windows 时)是,如果您尝试从 UNC 路径运行代码。映射驱动器号并运行映射的驱动器号也解决了这个问题。
回答by MiKlacko
I have the same issue as you and this was SOLUTION. ImageMagick was working correctly in terminal/console but not in nodejs (gm module). After 2 days of losing hair i fixed it by adding PATH variable to environment variables process.env.PATH There should be path to your imagemagick and other executables. Node.js has some PATH from system but for some reasone GM is ignoring it and using process.env.PATH
我和你有同样的问题,这是解决方案。ImageMagick 在终端/控制台中正常工作,但在 nodejs(gm 模块)中无法正常工作。脱发 2 天后,我通过将 PATH 变量添加到环境变量 process.env.PATH 来修复它,应该有你的 imagemagick 和其他可执行文件的路径。Node.js 有一些来自系统的 PATH 但出于某种原因 GM 忽略它并使用 process.env.PATH
I created environment variable PATH(process.env.PATH) and set value to bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin I'm using MAC OS X
我创建了环境变量 PATH(process.env.PATH) 并将值设置为 bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin 我使用的是 MAC OS X
I got imageMagick installed with brew (brew install imagemagick)
我用 brew ( brew install imagemagick)安装了 imageMagick
回答by Maik Greubel
Because I found this problem many times here on stackoverflow, I want to share this answer: https://stackoverflow.com/a/25461564/3970623
因为我在stackoverflow上多次发现这个问题,所以想分享这个答案:https: //stackoverflow.com/a/25461564/3970623
The "spawn ENOENT" seems to be caused by a valid unix tools installation which is accessible using PATH environment variable.
“spawn ENOENT”似乎是由可使用 PATH 环境变量访问的有效 unix 工具安装引起的。
回答by Gregor
In my case it was very simple. It ocurred rigth after instalation of GraphicsMagick in windows 10: I tryed using a console that was yet open before installing GraphicsMagick. Therefore it used old path information and didn′t found GraphicsMagick. Solution: I had to open a new console for running the node for using gm.
就我而言,这非常简单。它发生在 Windows 10 中安装 GraphicsMagick 之后:我尝试使用在安装 GraphicsMagick 之前尚未打开的控制台。因此它使用了旧的路径信息并且没有找到GraphicsMagick。解决方案:我必须打开一个新的控制台来运行节点以使用 gm。
回答by Naji Amer
/gm/lib/command.js have an option where you can set the appPath, if gm is already working through the terminal, you can get the path to gm and pass it via the subClass function,In my case gm was installed in /usr/local/bin/ using brew on MacOsx.
/gm/lib/command.js 有一个选项,您可以在其中设置 appPath,如果 gm 已经通过终端工作,您可以获取 gm 的路径并通过 subClass 函数传递它,在我的情况下 gm 安装在 / usr/local/bin/ 在 MacOsx 上使用 brew。
var gm = require('gm').subClass({ appPath: "/usr/local/bin/" });
回答by Chris Livdahl
Just in case someone is finding this error while on macOS, this worked for me:
以防万一有人在 macOS 上发现此错误,这对我有用:
$ brew install graphicsmagick

