Array
University of Oregon

npm: The Node Package Manager

Node comes with another program called the Node Package Manager (or NPM for short). This tool allows us to easily install Node modules (a.k.a., packages).

We can extend the core distribution of Node.js by adding packages using npm.


Windows Users: Run npm commands in Gitbash.

Node Packaged Modules

npm can install packages in local or global mode. In local mode it installs the package in a node_modules subdirectory in the current directory.

Update npm to the Latest Version

When you installed Node.js, that also installs npm. However, npm gets updated more frequently than Node, so you’ll want to make sure it’s the latest version:

npm install -g npm@latest
npm -v (version should be higher than 2.1.8)

Installing npm Packages in Global Mode

npm can install packages in local or global mode. When you use npm to install a package globally (with -g or –-global), npm uses a global directory on your computer owned by the root account on your system, not your user account, which can cause problems for packages installed globally.

Mac Users: To avoid these problems, follow the instructions given on NPM Global Packages before installing any global packages with npm.

Example:

In this example, a Node.js module called lodash is installed globally. (--global can be abbreviated to -g):

npm install --global lodash

List All Global Packages:

The output of the following command will confirm that lodash was installed in your home directory, in a subdirectory named .npm-global:

npm list -g --depth=0

Uninstalling Global Packages

Global packages can be uninstalled with npm uninstall. The following example uninstalls lodash:

npm uninstall -g lodash

Installing Packages in Local Mode

Installing packages in local mode is done without the --global or -g. The package will be installed in your current directory in a node_modules subdirectory.

This example shows how to install Express version 3 (by using the @ sign to append a version number):

cd ~/Documents/repos/281/examples/ch6/app/
npm install express@3
npm install jshint
ls 
node_modules

Finding the version of an installed npm package

npm show package version
(eg) npm show express version

Uninstalling Local Packages

Global packages can be uninstalled with npm uninstall -g. Simply omit the -g flag to uninstall local packages

npm uninstall jshint

Managing Dependencies: package.json

The best way to manage locally installed packages is to create a package.json file:

• It documents the packages your project depends on.
• You can share it with other developers, so they can build and run your app.

You can create a package.json file by running the npm init command:

npm init
This utility will walk you through creating a package.json file.
Press ^C at any time to quit.
name: (project) demo
version: (1.0.0)
description: Demo of package.json
entry point: (index.js)
test command:
git repository:
keywords:
author: your name here
license: (ISC)

This creates the following package.json file in the current directory:

{
  "name": "demo",
  "version": "1.0.0",
  "description": "Demo package.json",
  "main": "main.js",
  "dependencies": {
    "express": "^3.21.2",
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Susan Queue",
  "license": "ISC"
}

To avoid all the prompts, a quicker way to generate a package.json file is:

npm init --yes

How to Use package.json to Install Packages

Create a new directory and use your package.json file to install the dependencies.

cd ~/Documents/repos/281/examples/ch6/
mkdir package-demo
cd package-demo

Download package.json to your package-demo directory.

When you run the npm install command, npm reads the package.json file, and installs packages listed in the dependencies section:

npm install
npm list

Manually Editing package.json

You can open package.json in Atom and add dependencies manually, but the easier way to keep it up-to-date when you install new packages is to use the --save flag when installing packages.

npm install --save lodash

This installs the package and also updates package.json:

 "dependencies": {
     "express": "^4.15.3",
     "lodash": "^4.17.4"
 },

The carat (^) indicates a version range. "^3.21.2"  means any version in this range can be installed: 3.21.2  <= version < 4.0.0

Summary

  • npm makes it easy for JavaScript developers to share the code that they’ve created to solve particular problems, and for other developers to reuse that code in their own applications.
  • npm makes it easy to manage different versions of code, so it works well on projects managed with Git and Github.
  • There are thousands of packages on the npm registry.
Skip to toolbar