Last Updated: September 09, 2019
· nsmeta

Install an npm module in a Meteor app

If you haven't yet, create a Meteor app.

> meteor create demo-app
> cd demo-app

Since Meteor is built on top of Node.js, it uses its packaging system - npm.

> cd .meteor/local/build/server

Now install a module, like you would install it for any Node.js project. For example:

> npm install underscore

Now you can require a Node.js module in your Meteor app.

var require = __meteor_bootstrap__.require,
    underscore = require('underscore');

Note: This will work for server-side only. So either require the module in any of the files located in the *./server directory*, or surround that snippet like so:

if(Meteor.is_server) {
    var require = __meteor_bootstrap__.require,
        underscore = require('underscore');

4 Responses
Add your response

Thanks for the tip!

On Ubuntu 12.04 I had to use this trick to get it to install:

sudo PATH=${PATH} $(which npm) install pg

I had to use sudo because the .meteor/local/build/server/nodemodules is a symbolic link to /usr/lib/meteor/lib/nodemodules which is owned by root.

I had to use PATH=${PATH} construct because sudo on Ubuntu is configured to reset the PATH to a smal list, and hence my PATH, which contained path to pg_config (required to build 'pg'), was lost. Doing PATH=${PATH} made sudo retain my PATH in the sudoed environment.

I used $(which npm) because npm is not installed system-wide, and is actually installed and managed by nvm (Node Version Manager). So the $(which npm) gave the exact path that sudo culd use to execute the binary, else sudo can't find npm (even though PATH=$PATH should've done this).

over 1 year ago ·

My apologies, I tested this on OSX only. Great tip on symlink!

Though it's pretty weird that on my system .meteor(in the app's directory) isn't a symlink.

over 1 year ago ·

Cool tip, I would just be careful with some modules as meteor is fiberized.

over 1 year ago ·

After bundling my app I lose all of the node modules that I had and have to reinstall them everytime. How can I get around this as it can be tough to remember all of them

over 1 year ago ·