简体   繁体   English

错误:在节点中使用GM时产生ENOENT

[英]Error: spawn ENOENT while using GM in node

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.js file 该代码在postnewsitem.js文件中执行

why is this error occurring & how do I solve it ? 为什么会发生此错误,我该如何解决?

edit: GraphicsMagick works, proof: 编辑:GraphicsMagick的作品,证明:

在此处输入图片说明

Install ImageMagick and use subClass imageMagick. 安装ImageMagick并使用子类imageMagick。

  1. Install ImageMagick 安装ImageMagick

     sudo apt-get install imagemagick 
  2. using subClass imagemagick: 使用子类imagemagick:

     var gm = require('gm').subClass({ imageMagick: true }); 

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. 我在安装了gm和imagemagick的Windows 7上运行nodejs,似乎两个模块之间都存在冲突,所以我用Google搜索了一下,并找出了可以避免的方法。 I added this line and that solved my ENOENT problem: var imageMagick = gm.subClass({ imageMagick: true }); 我添加了这一行,从而解决了我的ENOENT问题: var imageMagick = gm.subClass({ imageMagick: true }); so the code now looks like this: 所以代码现在看起来像这样:

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

Had the same problem with Node.js application running on Windows using IIS. 使用IIS在Windows上运行的Node.js应用程序也存在相同的问题。 Problem has gone when I set " Load User Profile " option in " Advanced settings " of appropriate AppPool to " True " 当我在适当的AppPool的“ 高级设置 ”中将“ 加载用户配置文件 ”选项设置为“ ”时,问题就消失了

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. 注意:如果尚未安装imageMagick。 Please install that first 请先安装

Another scenario where this might happen (when using windows) is if you try to run your code from a UNC Path. 发生这种情况的另一种情况(使用Windows时)是如果您尝试从UNC路径运行代码。 mapping a drive letter and running over the mapped drive letter solves this problem as well. 映射驱动器号并在映射的驱动器号上运行也可以解决此问题。

I have the same issue as you and this was SOLUTION. 我和您有同样的问题,这就是解决方案。 ImageMagick was working correctly in terminal/console but not in nodejs (gm module). ImageMagick在终端/控制台中正常运行,但在nodejs(gm模块)中无法正常运行。 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. 经过两天的脱发,我通过将PATH变量添加到环境变量process.env.PATH来修复它。应该有imagemagick和其他可执行文件的路径。 Node.js has some PATH from system but for some reasone GM is ignoring it and using process.env.PATH 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 install imagemagick了imageMagick( brew install imagemagick

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”似乎是由有效的unix工具安装引起的,可以使用PATH环境变量进行访问。

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. 在Windows 10中安装GraphicsMagick之后,它出现了僵局:在安装GraphicsMagick之前,我尝试使用尚未打开的控制台。 Therefore it used old path information and didn´t found GraphicsMagick. 因此,它使用了旧的路径信息,但没有找到GraphicsMagick。 Solution: I had to open a new console for running the node for using gm. 解决方案:我必须打开一个新控制台来运行使用gm的节点。

/gm/lib/command.js有一个选项,您可以在其中设置appPath,如果gm已经在终端中工作,则可以获取gm的路径并通过subClass函数传递它,在我的情况下,gm安装在/ usr / local / bin /在MacOsx上使用brew。

var gm = require('gm').subClass({ appPath: "/usr/local/bin/" });

以防万一有人在macOS上发现此错误,这对我有用:

$ brew install graphicsmagick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM