Last Updated: September 09, 2019
·
24.08K
· benjaminrh

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

+1000

over 1 year ago ·

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 ·

Nice work!

over 1 year ago ·

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

over 1 year ago ·

Love this!

over 1 year ago ·

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 ·

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

over 1 year ago ·