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 Official Meteor Packaging System

meteor add meteorhacks:npm

Via Meteorite

mrt add npm

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.npmRequire()

Meteor.npmRequire() does not comes with the npm package you’ve installed via mrt. If so, please use Meteor.require() instead.

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

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 Official Meteor Packaging System

meteor add meteorhacks:npm

Via Meteorite

mrt add npm

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.npmRequire()

Meteor.npmRequire() does not comes with the npm package you’ve installed via mrt. If so, please use Meteor.require() instead.

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