1

I have a few related questions about node-gd and Meteor.

First couple of questions. I've tried to install node-gd in what I believe is the correct place.

Does this look like I've installed it to the correct location for use with Meteor?

Should I be worried about the warnings in the output?

me@ubuntu:/usr/local/lib$ sudo npm install node-gd
npm http GET https://registry.npmjs.org/node-gd
npm http 304 https://registry.npmjs.org/node-gd

> node-gd@0.2.3 install /usr/local/lib/node_modules/node-gd
> node-gyp rebuild

make: Entering directory `/usr/local/lib/node_modules/node-gd/build'
CXX(target) Release/obj.target/node_gd/cpp/node-gd.o

../cpp/node-gd.cpp: In static member function ‘static v8::Handle<v8::Value> Gd::Image::StringFTBBox(const v8::Arguments&)’:
../cpp/node-gd.cpp:1045:22: warning: variable ‘color’ set but not used [-Wunused-but-set-variable]
   REQ_INT_ARG(0, color);
                  ^
../cpp/node-gd.cpp:41:7: note: in definition of macro ‘REQ_INT_ARG’
int VAR;                                                              \
   ^
SOLINK_MODULE(target) Release/obj.target/node_gd.node
SOLINK_MODULE(target) Release/obj.target/node_gd.node: Finished
COPY Release/node_gd.node
make: Leaving directory `/usr/local/lib/node_modules/node-gd/build'
node-gd@0.2.3 node_modules/node-gd
me@ubuntu:/usr/local/lib$ ls
node_modules  python2.7  python3.4
me@ubuntu:/usr/local/lib$ cd node_modules/
me@ubuntu:/usr/local/lib/node_modules$ ls
meteorite  node-gd

I am passing coordinates back to the server and I want to use node-gd to manipulate an image on the server.

This is my Meteor method:

Meteor.methods({
  createImage: function(coords) {
  console.log('createImage')
  console.log(coords.x);

  var gd   = require('gd');
  }
});

When I try to run this function I get this on my terminal:

I20140826-06:44:18.166(-7)? Exception while invoking method 'createImage' ReferenceError: require is not defined
I20140826-06:44:18.166(-7)?     at Meteor.methods.createImage (app/server/server.js:7:15)
I20140826-06:44:18.167(-7)?     at maybeAuditArgumentChecks (packages/livedata/livedata_server.js:1487)
I20140826-06:44:18.167(-7)?     at packages/livedata/livedata_server.js:643
I20140826-06:44:18.168(-7)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20140826-06:44:18.168(-7)?     at packages/livedata/livedata_server.js:642
I20140826-06:44:18.168(-7)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20140826-06:44:18.168(-7)?     at _.extend.protocol_handlers.method (packages/livedata/livedata_server.js:641)
I20140826-06:44:18.168(-7)?     at packages/livedata/livedata_server.js:541

The answer to this question suggests various JS solutions. Is this what I need, can anyone recommend what's best to use for Meteor for both server and client?

Community
  • 1
  • 1
user1532669
  • 2,288
  • 4
  • 36
  • 72

2 Answers2

2

You can't add NPM modules to meteor this way, you should use the npm atmosphere package from meteorhacks : http://atmospherejs.com/package/npm

What you need to do is install the package via meteorite :

mrt add npm

Then add a packages.json in your project root and specify the node-gd dependency :

{
  "node-gd":"0.2.3"
}

Finally, in your server code use Meteor.require to access the node-gd API.

Be aware though that Meteor server side programming uses Fibers so you'll have to wrap async API calls to node-gd using either Meteor._wrapAsync or the set of Async utilities that come with the npm atmosphere package.

Here is a nice article on understanding this point : https://www.discovermeteor.com/blog/understanding-sync-async-javascript-node/

You can't use NPM packages on the client.

saimeunt
  • 22,666
  • 2
  • 56
  • 61
  • That's great, thank you so much. I'll give that a go, fingers crossed! – user1532669 Aug 26 '14 at 16:10
  • I'm now getting this error "Exception while invoking method 'createImage' TypeError: Object # has no method 'require'" .... can't see the node-gd lib installed anywhere. How can I check? How can I install the node-gd package if its not already installed? I've just upgraded to meteor 0.9.0 too, incase that's causing an issue. – user1532669 Aug 26 '14 at 18:11
  • I also tried Meteor.require('node-gd'); as that's the name of the package in packages.json – user1532669 Aug 26 '14 at 18:25
  • I've not yet digged into 0.9.0 issues, it is possible that it's causing problems. – saimeunt Aug 26 '14 at 18:40
  • No problem, I'll get this sorted eventually :) – user1532669 Aug 26 '14 at 18:52
  • Running "meteor add npm" seemed to sort it. That error has gone away now so looks like its working. :) – user1532669 Aug 26 '14 at 19:37
0

Try using Meteor.npmRequire('your module name')

Jackal
  • 2,591
  • 3
  • 25
  • 29