meteorhacks:npm

v1.5.0Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Use Npm Modules with Your Meteor App

Using With Meteor 1.2

If you are already using meteorhacks:npm and want to use Meteor 1.2 for your project. Follow these steps:

rm -rf packages/npm-container
meteor remove npm-container
meteor update meteorhacks:npm
meteor

With Meteor you only can use npm modules inside packages. You can't directly use npm modules with meteor apps. This package solves that issue :)

Installation

meteor add meteorhacks:npm

Then start your app with meteor and follow the instructions.

Defining Packages

Once the npm support has been initialized, you'll be having a file name called packages.json inside your app. Define packages on that file as shown below.

1{
2  "redis": "0.8.2",
3  "github": "0.1.8"
4}

You must need to define absolute version number of the npm module

If you need to install an npm module from a specific commit, use the syntax:

1{
2  "googleapis": "https://github.com/bradvogel/google-api-nodejs-client/archive/d945dabf416d58177b0c14da64e0d6038f0cc47b.tar.gz"
3}

Using Packages

You can use Meteor.npmRequire method to access the npm module on server side and use it as you want. Most of the npm modules provide asynchronous API's with callbacks or promises. So, you can't directly use them with Meteor. Because of that, this package comes with a handy set of Async utilities make your life easier.

Example on using npm module inside a Meteor method

1if (Meteor.isClient) {
2  getGists = function getGists(user, callback) {
3    Meteor.call('getGists', user, callback);
4  }
5}
6
7if (Meteor.isServer) {
8  Meteor.methods({
9    'getGists': function getGists(user) {
10      var GithubApi = Meteor.npmRequire('github');
11      var github = new GithubApi({
12          version: "3.0.0"
13      });
14
15      var gists = Async.runSync(function(done) {
16        github.gists.getFromUser({user: 'arunoda'}, function(err, data) {
17          done(null, data);
18        });
19      });
20
21      return gists.result;
22    }
23  });
24}

API

Available in the Server Side only

Meteor.npmRequire(npmModuleName)

This method loads NPM modules you've specified in the packages.json file.

1var Github = Meteor.npmRequire('github');

Meteor.require(npmModuleName)

Same as above. But deprecated.

Async Utilities

Available in the Server Side only Async Utitlies is available as a separate package via meteorhacks:async

Meteor APIs are executed synchronously. Most of the NodeJS modules works asynchronously. So we need a way to bride the gap. Async Utilities comes to rescue you.

Async.runSync(function)

Async.runSync() pause the execution until you invoke done() callback as shown below.

1var response = Async.runSync(function(done) {
2  setTimeout(function() { 
3    done(null, 1001);
4  }, 100);
5});
6
7console.log(response.result); // 1001

done() callback takes 2 arguments. error and the result object. You can get them as the return value of the Async.runSync() as shown as response in the above example.

return value is an object and it has 2 fields. error and result.

Meteor.sync(function)

Same as Async.runSync but deprecated.

Async.wrap(function)

Wrap an asynchronous function and allow it to be run inside Meteor without callbacks.

1
2//declare a simple async function
3function delayedMessge(delay, message, callback) {
4  setTimeout(function() {
5    callback(null, message);
6  }, delay);
7}
8
9//wrapping
10var wrappedDelayedMessage = Async.wrap(delayedMessge);
11
12//usage
13Meteor.methods({
14  'delayedEcho': function(message) {
15    var response = wrappedDelayedMessage(500, message);
16    return response;
17  }
18});

If the callback has a result, it will be returned from the wrapped function. If there is an error, it will be thrown.

Async.wrap(function) is very similar to Meteor._wrapAsync.

Async.wrap(object, functionName)

Very similar to Async.wrap(function), but this API can be used to wrap an instance method of an object.

1var github = new GithubApi({
2    version: "3.0.0"
3});
4
5//wrapping github.user.getFrom
6var wrappedGetFrom = Async.wrap(github.user, 'getFrom');

Async.wrap(object, functionNameList)

Very similar to Async.wrap(object, functionName), but this API can be used to wrap multiple instance methods of an object.

1var github = new GithubApi({
2    version: "3.0.0"
3});
4
5//wrapping github.user.getFrom and github.user.getEmails
6var wrappedGithubUser = Async.wrap(github.user, ['getFrom', 'getEmails']);
7
8//usage
9var profile = wrappedGithubUser.getFrom('arunoda');
10var emails = wrappedGithubUser.getEmails();