34

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:

enter image description here

The code is executed in the postnewsitem.js file

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

edit: GraphicsMagick works, proof:

enter image description here

Olivier_s_j
  • 5,490
  • 24
  • 80
  • 126
  • Did you install GraphicsMagick? Also, what platform are you using? GM [is only tested](https://github.com/aheckmann/gm/wiki/GraphicsMagick-and-ImageMagick-versions) on Mac OS X and Ubuntu. – robertklep Apr 25 '13 at 18:37
  • yes I have graphicsmagic 1.3.18 working on Lion OS X . ( Though it says: "imagemagick-6.8.0-10 already installed, it's just not linked", when trying to see if it is really installed.) – Olivier_s_j Apr 25 '13 at 18:42
  • GraphicsMagick isn't the same as ImageMagick. You need to [configure gm](http://aheckmann.github.io/gm/docs.html#imagemagick) to get it to work with ImageMagick. It looks like the `gm` module can't find the GM/IM binaries, they have to be somewhere in your `$PATH` I think. – robertklep Apr 25 '13 at 18:48
  • I installed graphicsmagic instead now, though the error remains. ( If I type gm in the terminal I see that it is installed.) – Olivier_s_j Apr 25 '13 at 18:53
  • 1
    off topic: @ojtwist , what IDE is the screenshot taken from ? – epeleg Jun 28 '13 at 08:25
  • Eclipse (which also allows you to debug it) – Olivier_s_j Jun 28 '13 at 13:16
  • do you happen to have a link to instructions on how to set up such an environment? I tried but was so far unsuccessful... – epeleg Aug 28 '13 at 06:29
  • Total beginner to node, I 'installed' GraphicsMagick using "npm install gm".. and I kept getting the 'spawn ENOENT' error when trying to upload/resize an image. After 'sudo apt-get install graphicsmagick' everything works great! I hope this saves someone much googling :) – iCreateAwesome Feb 05 '14 at 03:01

10 Answers10

67

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 });
    
Spudley
  • 166,037
  • 39
  • 233
  • 307
Quỳnh Lê
  • 671
  • 5
  • 2
  • Thanks, This resolved issue of my nodejs app on heroku. – Devesh Sep 04 '14 at 06:11
  • 1
    GM also requires [GraphicsMagick](http://www.graphicsmagick.org/README.html) like in GM's [Readme file](https://github.com/aheckmann/gm/blob/master/README.md) is mentioned. When I installed it, the issue disappeared. In Mac OS X, `brew install graphicsmagick`. – cespon Jan 12 '15 at 11:04
  • 1
    To CentOS guys : `yum install ImageMagick` – yangsibai Feb 06 '15 at 09:28
  • This should be be marked as the correct answer. Thanks a bunch! – jwanglof Nov 13 '15 at 13:18
  • sudo apt-get install imagemagick + sudo apt-get install graphicsmagick worked for me – user3014311 Jun 28 '18 at 01:05
10

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 });
Matiss
  • 5,079
  • 2
  • 27
  • 27
  • 1
    I can confirm that this solved my problem on OSX Mountain Lion. I installed it using macports instead of brew, In case this can help someone – roy riojas Oct 21 '13 at 18:48
4

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"

user2620800
  • 161
  • 1
  • 2
  • 7
  • OMG! Thank you sooooo much. Spent so long trying to figure this out. How you figured it out is beyond me, but thank you! – TYRONEMICHAEL Apr 29 '14 at 15:49
2

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

Surya Purohit
  • 1,090
  • 1
  • 9
  • 29
1

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.

epeleg
  • 10,347
  • 17
  • 101
  • 151
  • Can you give example? What do you mean with running over? – Matiss Aug 27 '13 at 10:08
  • maybe "running over" is not the right term. I meant that your node.exe should be run from something like `n:\node\node.exe` and not from something like `\\myserver\myshare\node\node.exe` – epeleg Aug 28 '13 at 06:32
1

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)

MiKlacko
  • 81
  • 1
  • 6
0

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.

Community
  • 1
  • 1
0

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.

Gregor
  • 1
  • 1
0

/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.

var gm = require('gm').subClass({ appPath: "/usr/local/bin/" });
Naji Amer
  • 61
  • 1
  • 3
0

Just in case someone is finding this error while on macOS, this worked for me:

$ brew install graphicsmagick
Chris Livdahl
  • 4,662
  • 2
  • 38
  • 33