1--- 2title: npm-install 3section: 1 4description: Install a package 5--- 6 7### Synopsis 8 9```bash 10npm install [<package-spec> ...] 11 12aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall 13``` 14 15### Description 16 17This command installs a package and any packages that it depends on. If the 18package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, 19the installation of dependencies will be driven by that, respecting the 20following order of precedence: 21 22* `npm-shrinkwrap.json` 23* `package-lock.json` 24* `yarn.lock` 25 26See [package-lock.json](/configuring-npm/package-lock-json) and 27[`npm shrinkwrap`](/commands/npm-shrinkwrap). 28 29A `package` is: 30 31* a) a folder containing a program described by a 32 [`package.json`](/configuring-npm/package-json) file 33* b) a gzipped tarball containing (a) 34* c) a url that resolves to (b) 35* d) a `<name>@<version>` that is published on the registry (see 36 [`registry`](/using-npm/registry)) with (c) 37* e) a `<name>@<tag>` (see [`npm dist-tag`](/commands/npm-dist-tag)) that 38 points to (d) 39* f) a `<name>` that has a "latest" tag satisfying (e) 40* g) a `<git remote url>` that resolves to (a) 41 42Even if you never publish your package, you can still get a lot of benefits 43of using npm if you just want to write a node program (a), and perhaps if 44you also want to be able to easily install it elsewhere after packing it up 45into a tarball (b). 46 47 48* `npm install` (in a package directory, no arguments): 49 50 Install the dependencies to the local `node_modules` folder. 51 52 In global mode (ie, with `-g` or `--global` appended to the command), 53 it installs the current package context (ie, the current working 54 directory) as a global package. 55 56 By default, `npm install` will install all modules listed as 57 dependencies in [`package.json`](/configuring-npm/package-json). 58 59 With the `--production` flag (or when the `NODE_ENV` environment 60 variable is set to `production`), npm will not install modules listed 61 in `devDependencies`. To install all modules listed in both 62 `dependencies` and `devDependencies` when `NODE_ENV` environment 63 variable is set to `production`, you can use `--production=false`. 64 65 > NOTE: The `--production` flag has no particular meaning when adding a 66 dependency to a project. 67 68* `npm install <folder>`: 69 70 If `<folder>` sits inside the root of your project, its dependencies will be installed and may 71 be hoisted to the top-level `node_modules` as they would for other 72 types of dependencies. If `<folder>` sits outside the root of your project, 73 *npm will not install the package dependencies* in the directory `<folder>`, 74 but it will create a symlink to `<folder>`. 75 76 > NOTE: If you want to install the content of a directory like a package from the registry instead of creating a link, you would need to use the `--install-links` option. 77 78 Example: 79 80 ```bash 81 npm install ../../other-package --install-links 82 npm install ./sub-package 83 ``` 84 85* `npm install <tarball file>`: 86 87 Install a package that is sitting on the filesystem. Note: if you just 88 want to link a dev directory into your npm root, you can do this more 89 easily by using [`npm link`](/commands/npm-link). 90 91 Tarball requirements: 92 * The filename *must* use `.tar`, `.tar.gz`, or `.tgz` as the 93 extension. 94 * The package contents should reside in a subfolder inside the tarball 95 (usually it is called `package/`). npm strips one directory layer 96 when installing the package (an equivalent of `tar x 97 --strip-components=1` is run). 98 * The package must contain a `package.json` file with `name` and 99 `version` properties. 100 101 Example: 102 103 ```bash 104 npm install ./package.tgz 105 ``` 106 107* `npm install <tarball url>`: 108 109 Fetch the tarball url, and then install it. In order to distinguish between 110 this and other options, the argument must start with "http://" or "https://" 111 112 Example: 113 114 ```bash 115 npm install https://github.com/indexzero/forever/tarball/v0.5.6 116 ``` 117 118* `npm install [<@scope>/]<name>`: 119 120 Do a `<name>@<tag>` install, where `<tag>` is the "tag" config. (See 121 [`config`](/using-npm/config#tag). The config's default value is `latest`.) 122 123 In most cases, this will install the version of the modules tagged as 124 `latest` on the npm registry. 125 126 Example: 127 128 ```bash 129 npm install sax 130 ``` 131 132 `npm install` saves any specified packages into `dependencies` by default. 133 Additionally, you can control where and how they get saved with some 134 additional flags: 135 136 * `-P, --save-prod`: Package will appear in your `dependencies`. This 137 is the default unless `-D` or `-O` are present. 138 139 * `-D, --save-dev`: Package will appear in your `devDependencies`. 140 141 * `-O, --save-optional`: Package will appear in your 142 `optionalDependencies`. 143 144 * `--no-save`: Prevents saving to `dependencies`. 145 146 When using any of the above options to save dependencies to your 147 package.json, there are two additional, optional flags: 148 149 * `-E, --save-exact`: Saved dependencies will be configured with an 150 exact version rather than using npm's default semver range operator. 151 152 * `-B, --save-bundle`: Saved dependencies will also be added to your 153 `bundleDependencies` list. 154 155 Further, if you have an `npm-shrinkwrap.json` or `package-lock.json` 156 then it will be updated as well. 157 158 `<scope>` is optional. The package will be downloaded from the registry 159 associated with the specified scope. If no registry is associated with 160 the given scope the default registry is assumed. See 161 [`scope`](/using-npm/scope). 162 163 Note: if you do not include the @-symbol on your scope name, npm will 164 interpret this as a GitHub repository instead, see below. Scopes names 165 must also be followed by a slash. 166 167 Examples: 168 169 ```bash 170 npm install sax 171 npm install githubname/reponame 172 npm install @myorg/privatepackage 173 npm install node-tap --save-dev 174 npm install dtrace-provider --save-optional 175 npm install readable-stream --save-exact 176 npm install ansi-regex --save-bundle 177 ``` 178 179 **Note**: If there is a file or folder named `<name>` in the current 180 working directory, then it will try to install that, and only try to 181 fetch the package by name if it is not valid. 182 183* `npm install <alias>@npm:<name>`: 184 185 Install a package under a custom alias. Allows multiple versions of 186 a same-name package side-by-side, more convenient import names for 187 packages with otherwise long ones, and using git forks replacements 188 or forked npm packages as replacements. Aliasing works only on your 189 project and does not rename packages in transitive dependencies. 190 Aliases should follow the naming conventions stated in 191 [`validate-npm-package-name`](https://www.npmjs.com/package/validate-npm-package-name#naming-rules). 192 193 Examples: 194 195 ```bash 196 npm install my-react@npm:react 197 npm install jquery2@npm:jquery@2 198 npm install jquery3@npm:jquery@3 199 npm install npa@npm:npm-package-arg 200 ``` 201 202* `npm install [<@scope>/]<name>@<tag>`: 203 204 Install the version of the package that is referenced by the specified tag. 205 If the tag does not exist in the registry data for that package, then this 206 will fail. 207 208 Example: 209 210 ```bash 211 npm install sax@latest 212 npm install @myorg/mypackage@latest 213 ``` 214 215* `npm install [<@scope>/]<name>@<version>`: 216 217 Install the specified version of the package. This will fail if the 218 version has not been published to the registry. 219 220 Example: 221 222 ```bash 223 npm install sax@0.1.1 224 npm install @myorg/privatepackage@1.5.0 225 ``` 226 227* `npm install [<@scope>/]<name>@<version range>`: 228 229 Install a version of the package matching the specified version range. 230 This will follow the same rules for resolving dependencies described in 231 [`package.json`](/configuring-npm/package-json). 232 233 Note that most version ranges must be put in quotes so that your shell 234 will treat it as a single argument. 235 236 Example: 237 238 ```bash 239 npm install sax@">=0.1.0 <0.2.0" 240 npm install @myorg/privatepackage@"16 - 17" 241 ``` 242 243* `npm install <git remote url>`: 244 245 Installs the package from the hosted git provider, cloning it with 246 `git`. For a full git remote url, only that URL will be attempted. 247 248 ```bash 249 <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>] 250 ``` 251 252 `<protocol>` is one of `git`, `git+ssh`, `git+http`, `git+https`, or 253 `git+file`. 254 255 If `#<commit-ish>` is provided, it will be used to clone exactly that 256 commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` 257 can be any valid semver range or exact version, and npm will look for 258 any tags or refs matching that range in the remote repository, much as 259 it would for a registry dependency. If neither `#<commit-ish>` or 260 `#semver:<semver>` is specified, then the default branch of the 261 repository is used. 262 263 If the repository makes use of submodules, those submodules will be 264 cloned as well. 265 266 If the package being installed contains a `prepare` script, its 267 `dependencies` and `devDependencies` will be installed, and the prepare 268 script will be run, before the package is packaged and installed. 269 270 The following git environment variables are recognized by npm and will 271 be added to the environment when running git: 272 273 * `GIT_ASKPASS` 274 * `GIT_EXEC_PATH` 275 * `GIT_PROXY_COMMAND` 276 * `GIT_SSH` 277 * `GIT_SSH_COMMAND` 278 * `GIT_SSL_CAINFO` 279 * `GIT_SSL_NO_VERIFY` 280 281 See the git man page for details. 282 283 Examples: 284 285 ```bash 286 npm install git+ssh://git@github.com:npm/cli.git#v1.0.27 287 npm install git+ssh://git@github.com:npm/cli#pull/273 288 npm install git+ssh://git@github.com:npm/cli#semver:^5.0 289 npm install git+https://isaacs@github.com/npm/cli.git 290 npm install git://github.com/npm/cli.git#v1.0.27 291 GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://git@github.com:npm/cli.git 292 ``` 293 294* `npm install <githubname>/<githubrepo>[#<commit-ish>]`: 295* `npm install github:<githubname>/<githubrepo>[#<commit-ish>]`: 296 297 Install the package at `https://github.com/githubname/githubrepo` by 298 attempting to clone it using `git`. 299 300 If `#<commit-ish>` is provided, it will be used to clone exactly that 301 commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` 302 can be any valid semver range or exact version, and npm will look for 303 any tags or refs matching that range in the remote repository, much as 304 it would for a registry dependency. If neither `#<commit-ish>` or 305 `#semver:<semver>` is specified, then the default branch is used. 306 307 As with regular git dependencies, `dependencies` and `devDependencies` 308 will be installed if the package has a `prepare` script before the 309 package is done installing. 310 311 Examples: 312 313 ```bash 314 npm install mygithubuser/myproject 315 npm install github:mygithubuser/myproject 316 ``` 317 318* `npm install gist:[<githubname>/]<gistID>[#<commit-ish>|#semver:<semver>]`: 319 320 Install the package at `https://gist.github.com/gistID` by attempting to 321 clone it using `git`. The GitHub username associated with the gist is 322 optional and will not be saved in `package.json`. 323 324 As with regular git dependencies, `dependencies` and `devDependencies` will 325 be installed if the package has a `prepare` script before the package is 326 done installing. 327 328 Example: 329 330 ```bash 331 npm install gist:101a11beef 332 ``` 333 334* `npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]`: 335 336 Install the package at `https://bitbucket.org/bitbucketname/bitbucketrepo` 337 by attempting to clone it using `git`. 338 339 If `#<commit-ish>` is provided, it will be used to clone exactly that 340 commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can 341 be any valid semver range or exact version, and npm will look for any tags 342 or refs matching that range in the remote repository, much as it would for a 343 registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is 344 specified, then `master` is used. 345 346 As with regular git dependencies, `dependencies` and `devDependencies` will 347 be installed if the package has a `prepare` script before the package is 348 done installing. 349 350 Example: 351 352 ```bash 353 npm install bitbucket:mybitbucketuser/myproject 354 ``` 355 356* `npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]`: 357 358 Install the package at `https://gitlab.com/gitlabname/gitlabrepo` 359 by attempting to clone it using `git`. 360 361 If `#<commit-ish>` is provided, it will be used to clone exactly that 362 commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can 363 be any valid semver range or exact version, and npm will look for any tags 364 or refs matching that range in the remote repository, much as it would for a 365 registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is 366 specified, then `master` is used. 367 368 As with regular git dependencies, `dependencies` and `devDependencies` will 369 be installed if the package has a `prepare` script before the package is 370 done installing. 371 372 Example: 373 374 ```bash 375 npm install gitlab:mygitlabuser/myproject 376 npm install gitlab:myusr/myproj#semver:^5.0 377 ``` 378 379You may combine multiple arguments and even multiple types of arguments. 380For example: 381 382```bash 383npm install sax@">=0.1.0 <0.2.0" bench supervisor 384``` 385 386The `--tag` argument will apply to all of the specified install targets. If 387a tag with the given name exists, the tagged version is preferred over 388newer versions. 389 390The `--dry-run` argument will report in the usual way what the install 391would have done without actually installing anything. 392 393The `--package-lock-only` argument will only update the 394`package-lock.json`, instead of checking `node_modules` and downloading 395dependencies. 396 397The `-f` or `--force` argument will force npm to fetch remote resources 398even if a local copy exists on disk. 399 400```bash 401npm install sax --force 402``` 403 404### Configuration 405 406See the [`config`](/using-npm/config) help doc. Many of the configuration 407params have some effect on installation, since that's most of what npm 408does. 409 410These are some of the most common options related to installation. 411 412#### `save` 413 414* Default: `true` unless when using `npm update` where it defaults to `false` 415* Type: Boolean 416 417Save installed packages to a `package.json` file as dependencies. 418 419When used with the `npm rm` command, removes the dependency from 420`package.json`. 421 422Will also prevent writing to `package-lock.json` if set to `false`. 423 424 425 426#### `save-exact` 427 428* Default: false 429* Type: Boolean 430 431Dependencies saved to package.json will be configured with an exact version 432rather than using npm's default semver range operator. 433 434 435 436#### `global` 437 438* Default: false 439* Type: Boolean 440 441Operates in "global" mode, so that packages are installed into the `prefix` 442folder instead of the current working directory. See 443[folders](/configuring-npm/folders) for more on the differences in behavior. 444 445* packages are installed into the `{prefix}/lib/node_modules` folder, instead 446 of the current working directory. 447* bin files are linked to `{prefix}/bin` 448* man pages are linked to `{prefix}/share/man` 449 450 451 452#### `install-strategy` 453 454* Default: "hoisted" 455* Type: "hoisted", "nested", "shallow", or "linked" 456 457Sets the strategy for installing packages in node_modules. hoisted 458(default): Install non-duplicated in top-level, and duplicated as necessary 459within directory structure. nested: (formerly --legacy-bundling) install in 460place, no hoisting. shallow (formerly --global-style) only install direct 461deps at top-level. linked: (experimental) install in node_modules/.store, 462link in place, unhoisted. 463 464 465 466#### `legacy-bundling` 467 468* Default: false 469* Type: Boolean 470* DEPRECATED: This option has been deprecated in favor of 471 `--install-strategy=nested` 472 473Instead of hoisting package installs in `node_modules`, install packages in 474the same manner that they are depended on. This may cause very deep 475directory structures and duplicate package installs as there is no 476de-duplicating. Sets `--install-strategy=nested`. 477 478 479 480#### `global-style` 481 482* Default: false 483* Type: Boolean 484* DEPRECATED: This option has been deprecated in favor of 485 `--install-strategy=shallow` 486 487Only install direct dependencies in the top level `node_modules`, but hoist 488on deeper dependencies. Sets `--install-strategy=shallow`. 489 490 491 492#### `omit` 493 494* Default: 'dev' if the `NODE_ENV` environment variable is set to 495 'production', otherwise empty. 496* Type: "dev", "optional", or "peer" (can be set multiple times) 497 498Dependency types to omit from the installation tree on disk. 499 500Note that these dependencies _are_ still resolved and added to the 501`package-lock.json` or `npm-shrinkwrap.json` file. They are just not 502physically installed on disk. 503 504If a package type appears in both the `--include` and `--omit` lists, then 505it will be included. 506 507If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment 508variable will be set to `'production'` for all lifecycle scripts. 509 510 511 512#### `include` 513 514* Default: 515* Type: "prod", "dev", "optional", or "peer" (can be set multiple times) 516 517Option that allows for defining which types of dependencies to install. 518 519This is the inverse of `--omit=<type>`. 520 521Dependency types specified in `--include` will not be omitted, regardless of 522the order in which omit/include are specified on the command-line. 523 524 525 526#### `strict-peer-deps` 527 528* Default: false 529* Type: Boolean 530 531If set to `true`, and `--legacy-peer-deps` is not set, then _any_ 532conflicting `peerDependencies` will be treated as an install failure, even 533if npm could reasonably guess the appropriate resolution based on non-peer 534dependency relationships. 535 536By default, conflicting `peerDependencies` deep in the dependency graph will 537be resolved using the nearest non-peer dependency specification, even if 538doing so will result in some packages receiving a peer dependency outside 539the range set in their package's `peerDependencies` object. 540 541When such an override is performed, a warning is printed, explaining the 542conflict and the packages involved. If `--strict-peer-deps` is set, then 543this warning is treated as a failure. 544 545 546 547#### `prefer-dedupe` 548 549* Default: false 550* Type: Boolean 551 552Prefer to deduplicate packages if possible, rather than choosing a newer 553version of a dependency. 554 555 556 557#### `package-lock` 558 559* Default: true 560* Type: Boolean 561 562If set to false, then ignore `package-lock.json` files when installing. This 563will also prevent _writing_ `package-lock.json` if `save` is true. 564 565 566 567#### `package-lock-only` 568 569* Default: false 570* Type: Boolean 571 572If set to true, the current operation will only use the `package-lock.json`, 573ignoring `node_modules`. 574 575For `update` this means only the `package-lock.json` will be updated, 576instead of checking `node_modules` and downloading dependencies. 577 578For `list` this means the output will be based on the tree described by the 579`package-lock.json`, rather than the contents of `node_modules`. 580 581 582 583#### `foreground-scripts` 584 585* Default: `false` unless when using `npm pack` or `npm publish` where it 586 defaults to `true` 587* Type: Boolean 588 589Run all build scripts (ie, `preinstall`, `install`, and `postinstall`) 590scripts for installed packages in the foreground process, sharing standard 591input, output, and error with the main npm process. 592 593Note that this will generally make installs run slower, and be much noisier, 594but can be useful for debugging. 595 596 597 598#### `ignore-scripts` 599 600* Default: false 601* Type: Boolean 602 603If true, npm does not run scripts specified in package.json files. 604 605Note that commands explicitly intended to run a particular script, such as 606`npm start`, `npm stop`, `npm restart`, `npm test`, and `npm run-script` 607will still run their intended script if `ignore-scripts` is set, but they 608will *not* run any pre- or post-scripts. 609 610 611 612#### `audit` 613 614* Default: true 615* Type: Boolean 616 617When "true" submit audit reports alongside the current npm command to the 618default registry and all registries configured for scopes. See the 619documentation for [`npm audit`](/commands/npm-audit) for details on what is 620submitted. 621 622 623 624#### `bin-links` 625 626* Default: true 627* Type: Boolean 628 629Tells npm to create symlinks (or `.cmd` shims on Windows) for package 630executables. 631 632Set to false to have it not do this. This can be used to work around the 633fact that some file systems don't support symlinks, even on ostensibly Unix 634systems. 635 636 637 638#### `fund` 639 640* Default: true 641* Type: Boolean 642 643When "true" displays the message at the end of each `npm install` 644acknowledging the number of dependencies looking for funding. See [`npm 645fund`](/commands/npm-fund) for details. 646 647 648 649#### `dry-run` 650 651* Default: false 652* Type: Boolean 653 654Indicates that you don't want npm to make any changes and that it should 655only report what it would have done. This can be passed into any of the 656commands that modify your local installation, eg, `install`, `update`, 657`dedupe`, `uninstall`, as well as `pack` and `publish`. 658 659Note: This is NOT honored by other network related commands, eg `dist-tags`, 660`owner`, etc. 661 662 663 664#### `cpu` 665 666* Default: null 667* Type: null or String 668 669Override CPU architecture of native modules to install. Acceptable values 670are same as `cpu` field of package.json, which comes from `process.arch`. 671 672 673 674#### `os` 675 676* Default: null 677* Type: null or String 678 679Override OS of native modules to install. Acceptable values are same as `os` 680field of package.json, which comes from `process.platform`. 681 682 683 684#### `libc` 685 686* Default: null 687* Type: null or String 688 689Override libc of native modules to install. Acceptable values are same as 690`libc` field of package.json 691 692 693 694#### `workspace` 695 696* Default: 697* Type: String (can be set multiple times) 698 699Enable running a command in the context of the configured workspaces of the 700current project while filtering by running only the workspaces defined by 701this configuration option. 702 703Valid values for the `workspace` config are either: 704 705* Workspace names 706* Path to a workspace directory 707* Path to a parent workspace directory (will result in selecting all 708 workspaces within that folder) 709 710When set for the `npm init` command, this may be set to the folder of a 711workspace which does not yet exist, to create the folder and set it up as a 712brand new workspace within the project. 713 714This value is not exported to the environment for child processes. 715 716#### `workspaces` 717 718* Default: null 719* Type: null or Boolean 720 721Set to true to run the command in the context of **all** configured 722workspaces. 723 724Explicitly setting this to false will cause commands like `install` to 725ignore workspaces altogether. When not set explicitly: 726 727- Commands that operate on the `node_modules` tree (install, update, etc.) 728will link workspaces into the `node_modules` folder. - Commands that do 729other things (test, exec, publish, etc.) will operate on the root project, 730_unless_ one or more workspaces are specified in the `workspace` config. 731 732This value is not exported to the environment for child processes. 733 734#### `include-workspace-root` 735 736* Default: false 737* Type: Boolean 738 739Include the workspace root when workspaces are enabled for a command. 740 741When false, specifying individual workspaces via the `workspace` config, or 742all workspaces via the `workspaces` flag, will cause npm to operate only on 743the specified workspaces, and not on the root project. 744 745This value is not exported to the environment for child processes. 746 747#### `install-links` 748 749* Default: false 750* Type: Boolean 751 752When set file: protocol dependencies will be packed and installed as regular 753dependencies instead of creating a symlink. This option has no effect on 754workspaces. 755 756 757 758### Algorithm 759 760Given a `package{dep}` structure: `A{B,C}, B{C}, C{D}`, 761the npm install algorithm produces: 762 763```bash 764A 765+-- B 766+-- C 767+-- D 768``` 769 770That is, the dependency from B to C is satisfied by the fact that A already 771caused C to be installed at a higher level. D is still installed at the top 772level because nothing conflicts with it. 773 774For `A{B,C}, B{C,D@1}, C{D@2}`, this algorithm produces: 775 776```bash 777A 778+-- B 779+-- C 780 `-- D@2 781+-- D@1 782``` 783 784Because B's D@1 will be installed in the top-level, C now has to install 785D@2 privately for itself. This algorithm is deterministic, but different 786trees may be produced if two dependencies are requested for installation in 787a different order. 788 789See [folders](/configuring-npm/folders) for a more detailed description of 790the specific folder structures that npm creates. 791 792### See Also 793 794* [npm folders](/configuring-npm/folders) 795* [npm update](/commands/npm-update) 796* [npm audit](/commands/npm-audit) 797* [npm fund](/commands/npm-fund) 798* [npm link](/commands/npm-link) 799* [npm rebuild](/commands/npm-rebuild) 800* [npm scripts](/using-npm/scripts) 801* [npm config](/commands/npm-config) 802* [npmrc](/configuring-npm/npmrc) 803* [npm registry](/using-npm/registry) 804* [npm dist-tag](/commands/npm-dist-tag) 805* [npm uninstall](/commands/npm-uninstall) 806* [npm shrinkwrap](/commands/npm-shrinkwrap) 807* [package.json](/configuring-npm/package-json) 808* [workspaces](/using-npm/workspaces) 809