Using npm to install or update required packages just like bundler for rubygems

Daniel Beardsley picture Daniel Beardsley · Feb 2, 2011 · Viewed 89.2k times · Source

I love Bundler, it's great at dependency management. I love npm, installing node packages is easy! I have a nodejs app and would love to be able to specify my apps dependencies and easily install / update them wherever I deploy my app. This isn't a library I'm releasing, it's a full fledged web-app.

I'm aware of the npm bundle command, but that just seems to simply override the directory where packages are installed.

I'm used to using bundler in this fashion:

# Gemfile
gem "rails", "3.0.3"

Installs rails v3.0.3 and any other required gems on the host machine only if it doesn't already exist

> bundle install

How can I achieve something similar with npm?

Answer

isaacs picture isaacs · May 16, 2011

As of npm 1.0 (which is now what you get by default if you follow the steps in the README file), "bundle" is no longer a segregated thing -- it's just "how it works".

So:

  1. Put a package.json file in the root of your project
  2. List your deps in that file

    { "name" : "my-project"
    , "version" : "1.0.0"
    , "dependencies" : { "express" : "1.0.0" } }
    
  3. npm install Since you're calling this with no args, and not in global mode, it'll just install all your deps locally.

  4. require("express") and be happy.