Node.js – Managing Dependancies

Similar again to the Ruby Gemspec, Node.js has a file that manages all the dependancies for the project.

This is managed through a file called package.json.  In the json formatted file, you simply add the dependancies you’re wanting in the project, along with the version.

Here’s an example:

{
  "name": "NodeJS",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.12.0",
    "cookie-parser": "~1.3.4",
    "debug": "~2.1.1",
    "express": "~4.12.2",
    "jade": "~1.9.2",
    "morgan": "~1.5.1",
    "serve-favicon": "~2.2.0"
  }
}

Notice in the above example, that the versions are specified.  If you want the latest version, you can simply write “latest” instead of version, or use a star, like “*”.

After you have updated your package.json file, go back at the command prompt and move to the folder of your project.  Run the command: npm install 

This starts installing each item in the package.json dependancies. If you’re familiar with Ruby, this is very similar to bundle.

Verifying NPM Installs

To verify what was installed via NPM, you can run the command:

npm ls

This will output a tree diagram of what is installed.  Here’s an example from my console:

Screen Shot 2015-03-19 at 12.14.39 PM

 

Leave a Reply

Your email address will not be published. Required fields are marked *