简体   繁体   中英

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

why is this error occurring & how do I solve it ?

edit: GraphicsMagick works, proof:

在此处输入图片说明

Install ImageMagick and use subClass imageMagick.

  1. Install ImageMagick

     sudo apt-get install imagemagick 
  2. using subClass 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. I added this line and that solved my ENOENT problem: 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:

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

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 "

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

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.

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

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

I got imageMagick installed with brew ( 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

The "spawn ENOENT" seems to be caused by a valid unix tools installation which is accessible using PATH environment variable.

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.

/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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