srvdta
Last Updated: August 09, 2016
·
21.51K
· benjaminrh
2aca501921a5a98762cc4af16da258b8

Using NPM modules in Meteor

I've been seeing a lot of people being confused about using NPM modules in Meteor recently. As you may know, Meteor added support for this in v0.6.0. And it's very easy. Onward!

Let's use the foobar NPM module. Gotta love the classics! First, create a packages directory in the root of your Meteor project, which can store all of the NPM modules you want to use. Inside that, create a foobar directory.

We'll add two files to the /packages/foobar directory. Firstly, the package.js file. If you've made or looked at a Meteor/Meteorite smart package, this will look familiar, with one addition. So:

Npm.depends({
    'foobar': 'x.x.x' // Where x.x.x is the version, e.g. 0.3.2
});

Package.on_use(function (api) {
    api.add_files('foobar.js', 'server'); // Or 'client', or ['server', 'client']
});

Now that we've specified the module and version and the files we want from the module and where to include them (whew), let's make another file! Remember I said there would be two? We can call this one foobar.js or something.

Foobar = Npm.require("foobar");

I love one-liners too. And that's it! Now you can use Foobar in your code.

7 Responses
Add your response

7128
Profile small

+1000

over 1 year ago ·
7162
Ab13df38843556b57f7d2f6fe78003cf

I just make this little step further and wrap whole this as a Smart Package

mrt add npm

Now it is possible to provide NPM dependencies via packages.json file from your project root.

See my article on Complete NPM Integration for Meteor

over 1 year ago ·
7165
2aca501921a5a98762cc4af16da258b8

Nice work!

over 1 year ago ·
8482
17e829f5b7cfffe8bc0fd706cf2edf2c

I like, does this mean the actual NPM module is downloaded by Meteor (Or the atmospere npm package)?

over 1 year ago ·
9154
69bdacd70dafc570b4d2cc1e670b1968

Love this!

over 1 year ago ·
11615
9b467801e77bf6f782c08e88bb4c1a3f

I had no luck with doing it manually as you describe. It gives me

ReferenceError: ExcelExport is not defined
(tried to add 'excel-export' package)

Your "npm" package did the trick until yesterday when Meteor 0.7.0 was released. THe module seems to be broken now?

npm ERR! missing: rimraf@2.x, required by meteor-npm@0.1.10
npm ERR! missing: mkdirp@0.3.x, required by meteor-npm@0.1.10

over 1 year ago ·
14636
5b70c729506eeb6ff1d5b277eeb0f2da

How do you not specify a particular version, e.g. if you want the latest?

over 1 year ago ·