32

I am trying to convert SVG to PNG with node js. My code is here:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'image/png'});
  var convert = child_proc.spawn("convert", ["svg:", "png:-"]),
      values = (url.parse(req.url, true).query['values'] || ".5,.5")
        .split(",")
        .map(function(v){return parseFloat(v)});

  convert.stdout.on('data', function (data) {
    res.write(data);
  });
  convert.on('exit', function(code) {
    res.end();
  });

  jsdom.env({features:{QuerySelector:true}, html:htmlStub, scripts:scripts, done:function(errors, window) {
    var svgsrc = window.insertPie("#pie", w, h, values).innerHTML;
    //jsdom's domToHTML will lowercase element names
    svgsrc = svgsrc.replace(/radialgradient/g,'radialGradient');
    convert.stdin.write(svgsrc);
    convert.stdin.end();
  }});
}).listen(8888);

While executing I got this error (in MAC)

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

I have specified the path for nodejs. But i dont know why it fails. Any idea about this issue?

sprabhakaran
  • 1,455
  • 4
  • 17
  • 34

3 Answers3

30

It's likely failing because it can't find the convert application. Does the path to convert exist in your environment PATH? Can you run convert from your terminal?

badsyntax
  • 8,401
  • 3
  • 38
  • 58
  • 4
    Derp. I did not have imagemagick installed. and was using mongoose-thumbnail. Thanks! – Yablargo Apr 05 '14 at 04:56
  • 5
    For others, see also [this issue](http://stackoverflow.com/questions/17516772/using-nodejss-spawn-causes-unknown-option-and-error-spawn-enoent-err). Simply replacing `spawn` by `exec` fixed all of my problems (linux binary in PATH with permissions wouldn't execute). – msanford Apr 08 '14 at 15:28
  • Is there a way to specify that dependency so that npm install tells the so to install the package? – krusty.ar Sep 10 '14 at 15:10
8

I was getting the error

Uncaught Error: spawn myExeCommand ENOENT

Once I added 'options' to the spawn(), it worked.

let options = {shell: true};
let theProcess = child_process.spawn(myExeCommand, null, options);
Sagan
  • 1,043
  • 1
  • 10
  • 9
1

I had this same issue running from Linux. I did the npm install unoconv and thought that that would take care of installing the convert application, but only when I had installed it could I get it to run in Node.js sudo apt-get install unoconv

Lance
  • 115
  • 1
  • 2
  • 12