Npm and the package.json file

In this blogpost I aim to give a quick and practical explanation of my most used npm commands. It's a beginner's guide, I suggest you take a look at the npm documenation if you are looking for more detailed and complete information.

What is npm?

npm- the Node Package Manager- is crucial when developing applications in node.

npm is the official package manager for Node.js. As of Node.js version 0.6.3, npm is bundled and installed automatically with the environment. npm runs through the command line and manages dependencies for an application. It also allows users to install Node.js applications that are available on the npm registry.
- Wikipedia

What is a package.json file ?

All npm packages contain a file, usually in the project root, called package.json. This file holds various metadata relevant to the project. It is used to give information to npm that allows it to identify the project as well as handle the project's dependencies. It can also contain other metadata such as a project description, the version of the project, license information, even configuration data - all of which can be vital to both npm and to the end users of the package.

Take look at the following link for a detailed and interactive look at the package.json file: http://package.json.nodejitsu.com/

Installing a global package

npm install -g grunt-cli

npm will install packages locally by default. Use --global or -g on any command to operate in global mode instead. Global packages are installed in %appdata%\npm.

Initializing a package.json file

npm init

This command will interactively create a package.json file.
Besides asking you question, the init command will make guesses about what you want things to be set to. If you allready have a package.json file, it'll read that first and default to the options in there.

npm install

npm install

This command will install all packages listed in the package.json file. If a version is supplied, npm will install that specific version, otherwise it will install the most recent one.

npm install express

Install the Express package locally.

npm install express --save

Install Express locally and add it to the dependencies section in the package.json file.

npm install express --save-dev

Install Express locally and add it to the dev-dependencies section in the package.json file.

npm update

npm update

Update all packages found in the package.json file to the latest version. Specify the -g tag to update globally installed packages.
Add the --save or --save-dev tags to update the package.json file with the latest versions.

Many more

npm has loads of features and configuration settings, way to much to all document here. Do you use a feature often and it is not listed here? Add a comment below and I'll be sure to update the blogpost!

You might enjoy


I've been playing around with Angular the last couple of weeks. Some functionalities are really easy to implement. Others…

I often see the same type of problems arise when people try to learn JavaScript. The parts that make…

Tim Sommer

I'm a Web Developer with +7 years of experience in the .Net framework, with a focus on HTML5 and JavaScript.

Belgium, Antwerp
comments powered by Disqus
OWIC
Member of The Internet Defense League