npm-install

Install a package

Table of contents

Synopsis

npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <alias>@npm:<name>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>

aliases: npm i, npm add
common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional|--save-peer] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]

Description

This command installs a package and any packages that it depends on. If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that, respecting the following order of precedence:

See package-lock.json and npm shrinkwrap.

A package is:

Even if you never publish your package, you can still get a lot of benefits of using npm if you just want to write a node program (a), and perhaps if you also want to be able to easily install it elsewhere after packing it up into a tarball (b).

You may combine multiple arguments and even multiple types of arguments. For example:

npm install sax@">=0.1.0 <0.2.0" bench supervisor

The --tag argument will apply to all of the specified install targets. If a tag with the given name exists, the tagged version is preferred over newer versions.

The --dry-run argument will report in the usual way what the install would have done without actually installing anything.

The --package-lock-only argument will only update the package-lock.json, instead of checking node_modules and downloading dependencies.

The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk.

npm install sax --force

Configuration

See the config help doc. Many of the configuration params have some effect on installation, since that’s most of what npm does.

These are some of the most common options related to installation.

Configuration Options Affecting Dependency Resolution And Tree Design

Omitting Dependency Types

You may omit certain types of dependencies by using the --omit=<type> config option. This may be specified multiple types on the command line. To enter omit options in .npmrc files, use the following syntax:

omit[] = dev
omit[] = optional
; etc...

The dependency types that may be omitted or included are:

To re-include dependency, use the --include option, which may also be specified multiple times.

Legacy shorthands for omit settings are:

Configuration Options Affecting Build Process

Algorithm

Given a package{dep} structure: A{B,C}, B{C}, C{D}, the npm install algorithm produces:

A
+-- B
+-- C
+-- D

That is, the dependency from B to C is satisfied by the fact that A already caused C to be installed at a higher level. D is still installed at the top level because nothing conflicts with it.

For A{B,C}, B{C,D@1}, C{D@2}, this algorithm produces:

A
+-- B
+-- C
   `-- D@2
+-- D@1

Because B’s D@1 will be installed in the top-level, C now has to install D@2 privately for itself. This algorithm is deterministic, but different trees may be produced if two dependencies are requested for installation in a different order.

See folders for a more detailed description of the specific folder structures that npm creates.

See Also