8

In JS I require in the node module gm (which I want to use with imageMagick rather than the default graphicsMagick) while passing an argument like this:

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

How can I do that in ES6?

import gm from "gm";
gm.subClass({ imageMagick: true });

doesn't work, because gm defaults to GraphicsMagick which isn't installed.

r0bs
  • 297
  • 2
  • 9
  • 1
    In your Node example, you save the return value of `subClass` into a variable. Do you do that in your ES6 code? – apsillers Feb 01 '16 at 14:01
  • Can you elaborate on "it doesn't work"? – Felix Kling Feb 01 '16 at 14:16
  • @FelixKling: It defaults to graphicsMagick, which isn't installed and therefore logs `[Error: Could not execute GraphicsMagick/ImageMagick: gm "convert" "-size" "640x360" "xc:#f9b005" "-fill" "#ffffff" "-pointsize" "140" "-draw" "gravity center text 0,0 \"img\"" "/tmp/img.jpg" this most likely means the gm/convert binaries can't be found]` – r0bs Feb 01 '16 at 15:03
  • Did you try to use the *return value* of `subClass`, just like you do in the first example? `const im = gm.subClass({ imageMagick: true }); /* use I'm here */`. – Felix Kling Feb 01 '16 at 15:04
  • @FelixKling great! that works! thank u! – r0bs Feb 01 '16 at 15:10

1 Answers1

10

Answer from @Felix Kling works:

import gm from "gm";
const im = gm.subClass({ imageMagick: true });

...using im from here on!

r0bs
  • 297
  • 2
  • 9
  • I'm using gm in an AWS lambda (via Typescript/webpack). Seems to give runtime errors: `module initialization error: TypeError` - the compiled code is as such: `const gm_1 = __webpack_require__(17); const IMAGEMAGICK = gm_1.default.subClass({ imageMagick: true });`. Very weird, will keep investigating. – Matt Rowles Oct 04 '17 at 21:25
  • Removing the '.default.' fixed the above problem...not sure how to fix this properly though. `const IMAGEMAGICK = gm_1.subClass({ imageMagick: true });` – Matt Rowles Oct 05 '17 at 07:27
  • 1
    For those coming late, this fixed my problem in the above comments: `import * as GM from 'gm';` – Matt Rowles Oct 08 '17 at 23:57