Kadira - Performance Monitoring for Meteor (you should try this)

Complete NPM integration for Meteor

Npm support for Meteor comes to the light from version 0.6.0. But it gives complete NPM access to packages only. If you need to use npm modules like redis, aws-sdk, colors, winston in your app, you are out of luck.

Of course, you can wrap npm modules in a package or use if it is available on atmosphere, but that’s kind a hard.

Here comes the solution

I could be able to trick meteor a bit, and now we can have complete access to npm modules from Meteor. Here’s how you can do it. It’s pretty simple.

Adding NPM support to your app

Via Meteorite

mrt add npm

If you are working on multiple meteor projects at the sametime or using different versions, try to use following method instead installing it with meteorite

Via NPM

npm install -g meteor-npm #single time operation
meteor-npm #type inside your project

This creates a package named npm inside your project and it has no link with meteorite. It is also included in your git.
With this, you can use npm in multiple meteor projects without a problem, regardless of their versions.

Create packages.json file

Then create packages.json file on your project root.

Note that it is packages.json, not package.json

Now define npm packages you want, with the absolute package versions as shown below.

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

Let’s use a npm module

Normally you are loading core npm modules using Npm.require(), But in order to load modules from your packages.json you need to use Meteor.require()

Let’s get some gists using the github npm module.

var Github = Meteor.require('github');
var github = new Github();

github.gists.getFromUser({user: 'arunoda'}, function(err, gists) {
  console.log(gists);
});

Using npm modules within Meteor APIs

Meteor server side API’s are executed synchronously. But most of the npm modules work asynchronously. Although we can load NPM modules to meteor, it is so hard to use them inside Meteor APIs like methods, publications, permissions.

This is not a very big problem. I could be able to fix this by building a set of Async Utilities.

See the following example where I used a npm module inside a Meteor Method

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

if (Meteor.isServer) {
  var GithubApi = Meteor.require('github');
  var github = new GithubApi({
      version: "3.0.0"
  });

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

      return gists.result;
    }
  });
}

Using meteor bundle and demeteorizer

Modules you’ve added will be included in the bundled version(meteor bundle) automatically.

But if you’ve used a binary npm module, you need to re-install it manually. Just like you are doing it with fibers.
If you are using demeteorizer, then there is no problem.

Meteor is now open for 30000+ npm modules

Today I bring 30000+ packages(modules) to Meteor. Use them and share your thoughts. I’m always open minded for new ideas.