L.T
L.T

Reputation: 2589

How can I use NodeJS modules in Meteor?

In a NodeJS application ,I have finished some modules,Now I want to use them in Meteor,What should I do? For example,there is a file 'hello.js',content:

require('url');// In here,require other modules
function sayHi(name){
       console.log("Hi "+ name);
}
exports.sayHi = sayHi;

How do I use 'say Hi' in meteor?

when I do this:

if (Meteor.isServer) {
       Meteor.startup(function () {
       var require = __meteor_bootstrap__.require;
       var index = require('./hello');
       hello.syaHi('Ec');})}

Errors is:


app/index.js:1
require();
^
ReferenceError: require is not defined
    at app/index.js:1:1
    at /home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:113:21
    at Array.forEach (native)
    at Function._.each._.forEach (/usr/lib/meteor/lib/node_modules/underscore/underscore.js:79:11)
    at run (/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:99:7)

Upvotes: 1

Views: 1436

Answers (4)

zeroasterisk
zeroasterisk

Reputation: 2209

Here's a package which makes it a lot easier to use NPM packages inside Meteor:

https://github.com/meteorhacks/npm

example usage:

if (Meteor.isClient) {
  getGists = function getGists(user, callback) {
    Meteor.call('getGists', user, callback);
  }
}

if (Meteor.isServer) {
  Meteor.methods({
    'getGists': function getGists(user) {
      var GithubApi = Meteor.npmRequire('github');
      var github = new GithubApi({
          version: "3.0.0"
      });

      var gists = Async.runSync(function(done) {
        github.gists.getFromUser({user: 'arunoda'}, function(err, data) {
          done(null, data);
        });
      });

      return gists.result;
    }
  });
}

Upvotes: 0

jasongonzales
jasongonzales

Reputation: 1597

Update, I had to install my module into .meteor/local/build/programs/server/node_modules as well as use Npm.require.

Upvotes: 0

Will Kessler
Will Kessler

Reputation: 565

Also, it looks like Npm.require() is the right way to require node modules now.

Upvotes: 2

net.user
net.user

Reputation: 831

I think, you have to install/copy your module into projectdir/.meteor/local/build/server/node_modules which is a link to /usr/local/meteor/lib/node_modules. I tried this with the node.js module tracer and it worked. You have to copy your files into this directory every time you updated your meteor installation.

Upvotes: 2

Related Questions