@@ -29,10 +29,17 @@ common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exa
29
29
30
30
### Description
31
31
32
- This command installs a package, and any packages that it depends on. If the
33
- package has a package-lock or shrinkwrap file, the installation of dependencies
34
- will be driven by that, with an ` npm-shrinkwrap.json ` taking precedence if both
35
- files exist. See [ package-lock.json] ( /configuring-npm/package-lock-json ) and [ ` npm shrinkwrap ` ] ( /cli-commands/shrinkwrap ) .
32
+ This command installs a package and any packages that it depends on. If the
33
+ package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the
34
+ installation of dependencies will be driven by that, respecting the following
35
+ order of precedence:
36
+
37
+ * ` node_modules/.package-lock.json `
38
+ * ` npm-shrinkwrap.json `
39
+ * ` package-lock.json `
40
+ * ` yarn.lock `
41
+
42
+ See [ package-lock.json] ( /configuring-npm/package-lock-json ) and [ ` npm shrinkwrap ` ] ( /cli-commands/shrinkwrap ) .
36
43
37
44
A ` package ` is:
38
45
@@ -50,9 +57,9 @@ perhaps if you also want to be able to easily install it elsewhere
50
57
after packing it up into a tarball (b).
51
58
52
59
53
- * ` npm install ` (in package directory, no arguments):
60
+ * ` npm install ` (in a package directory, no arguments):
54
61
55
- Install the dependencies in the local node_modules folder.
62
+ Install the dependencies in the local ` node_modules ` folder.
56
63
57
64
In global mode (ie, with ` -g ` or ` --global ` appended to the command),
58
65
it installs the current package context (ie, the current working
@@ -75,13 +82,13 @@ after packing it up into a tarball (b).
75
82
Install the package in the directory as a symlink in the current project.
76
83
Its dependencies will be installed before it's linked. If ` <folder> ` sits
77
84
inside the root of your project, its dependencies may be hoisted to the
78
- toplevel ` node_modules ` as they would for other types of dependencies.
85
+ top-level ` node_modules ` as they would for other types of dependencies.
79
86
80
87
* ` npm install <tarball file> ` :
81
88
82
89
Install a package that is sitting on the filesystem. Note: if you just want
83
90
to link a dev directory into your npm root, you can do this more easily by
84
- using ` npm link ` .
91
+ using [ ` npm link ` ] ( /cli-commands/npm-link ) .
85
92
86
93
Tarball requirements:
87
94
* The filename * must* use ` .tar ` , ` .tar.gz ` , or ` .tgz ` as
@@ -114,24 +121,6 @@ after packing it up into a tarball (b).
114
121
115
122
npm install sax
116
123
117
- * ` npm install <alias>@npm:<name> ` :
118
-
119
- Install a package under a custom alias. Allows multiple versions of
120
- a same-name package side-by-side, more convenient import names for
121
- packages with otherwise long ones and using git forks replacements
122
- or forked npm packages as replacements. Aliasing works only on your
123
- project and does not rename packages in transitive dependencies.
124
- Aliases should follow the naming conventions stated in
125
- [ ` validate-npm-package-name ` ] ( https://www.npmjs.com/package/validate-npm-package-name#naming-rules ) .
126
-
127
- Examples:
128
-
129
- npm install my-react@npm:react
130
- npm install jquery2@npm:jquery@2
131
- npm install jquery3@npm:jquery@3
132
- npm install npa@npm:npm-package-arg
133
-
134
-
135
124
` npm install ` saves any specified packages into ` dependencies ` by default.
136
125
Additionally, you can control where and how they get saved with some
137
126
additional flags:
@@ -181,6 +170,24 @@ after packing it up into a tarball (b).
181
170
working directory, then it will try to install that, and only try to
182
171
fetch the package by name if it is not valid.
183
172
173
+ * ` npm install < alias> @npm:< name> ` :
174
+
175
+ Install a package under a custom alias. Allows multiple versions of
176
+ a same-name package side-by-side, more convenient import names for
177
+ packages with otherwise long ones, and using git forks replacements
178
+ or forked npm packages as replacements. Aliasing works only on your
179
+ project and does not rename packages in transitive dependencies.
180
+ Aliases should follow the naming conventions stated in
181
+ [` validate-npm-package-name` ](https://www.npmjs.com/package/validate-npm-package-name#naming-rules).
182
+
183
+ Examples:
184
+
185
+ npm install my-react@npm:react
186
+ npm install jquery2@npm:jquery@2
187
+ npm install jquery3@npm:jquery@3
188
+ npm install npa@npm:npm-package-arg
189
+
190
+
184
191
* ` npm install [< @scope> /]< name> @< tag> ` :
185
192
186
193
Install the version of the package that is referenced by the specified tag.
@@ -217,7 +224,7 @@ after packing it up into a tarball (b).
217
224
Example:
218
225
` ` ` bash
219
226
npm install sax@" >=0.1.0 <0.2.0"
220
- npm install @myorg/privatepackage@" >=0.1.0 <0.2.0 "
227
+ npm install @myorg/privatepackage@" 16 - 17 "
221
228
` ` `
222
229
223
230
* ` npm install < git remote url> ` :
@@ -263,6 +270,7 @@ after packing it up into a tarball (b).
263
270
264
271
` ` ` bash
265
272
npm install git+ssh://git@github.com:npm/cli.git#v1.0.27
273
+ npm install git+ssh://git@github.com:npm/cli#pull/273
266
274
npm install git+ssh://git@github.com:npm/cli#semver:^5.0
267
275
npm install git+https://isaacs@github.com/npm/cli.git
268
276
npm install git://github.com/npm/cli.git#v1.0.27
@@ -283,7 +291,7 @@ after packing it up into a tarball (b).
283
291
specified, then ` master` is used.
284
292
285
293
As with regular git dependencies, ` dependencies` and ` devDependencies` will
286
- be installed if the package has a ` prepare` script, before the package is
294
+ be installed if the package has a ` prepare` script before the package is
287
295
done installing.
288
296
289
297
Examples:
@@ -300,7 +308,7 @@ after packing it up into a tarball (b).
300
308
optional and will not be saved in ` package.json` .
301
309
302
310
As with regular git dependencies, ` dependencies` and ` devDependencies` will
303
- be installed if the package has a ` prepare` script, before the package is
311
+ be installed if the package has a ` prepare` script before the package is
304
312
done installing.
305
313
306
314
Example:
@@ -322,7 +330,7 @@ after packing it up into a tarball (b).
322
330
specified, then ` master` is used.
323
331
324
332
As with regular git dependencies, ` dependencies` and ` devDependencies` will
325
- be installed if the package has a ` prepare` script, before the package is
333
+ be installed if the package has a ` prepare` script before the package is
326
334
done installing.
327
335
328
336
Example:
@@ -344,7 +352,7 @@ after packing it up into a tarball (b).
344
352
specified, then ` master` is used.
345
353
346
354
As with regular git dependencies, ` dependencies` and ` devDependencies` will
347
- be installed if the package has a ` prepare` script, before the package is
355
+ be installed if the package has a ` prepare` script before the package is
348
356
done installing.
349
357
350
358
Example:
@@ -354,7 +362,7 @@ after packing it up into a tarball (b).
354
362
npm install gitlab:myusr/myproj#semver:^5.0
355
363
` ` `
356
364
357
- You may combine multiple arguments, and even multiple types of arguments.
365
+ You may combine multiple arguments and even multiple types of arguments.
358
366
For example:
359
367
360
368
` ` ` bash
@@ -378,10 +386,6 @@ local copy exists on disk.
378
386
npm install sax --force
379
387
` ` `
380
388
381
- The ` --no-fund` argument will hide the message displayed at the end of each
382
- install that acknowledges the number of dependencies looking for funding.
383
- See ` npm-fund(1)`
384
-
385
389
The ` -g` or ` --global` argument will cause npm to install the package globally
386
390
rather than locally. See [folders](/configuring-npm/folders).
387
391
@@ -398,17 +402,29 @@ The `--legacy-bundling` argument will cause npm to install the package such
398
402
that versions of npm prior to 1.4, such as the one included with node 0.8,
399
403
can install the package. This eliminates all automatic deduping.
400
404
405
+ The ` --legacy-peer-deps` argument will cause npm to ignore all
406
+ ` peerDependencies` when installing, similar to npm@6 and older.
407
+
401
408
The ` --link` argument will cause npm to link global installs into the
402
- local space in some cases.
409
+ local space whenever packages from the global space may satisfy a dependency
410
+ required version.
411
+
412
+ The ` --no-audit` argument can be used to disable sending audit reports to
413
+ the configured registries. See [` npm-audit` ](npm-audit) for details on what
414
+ is sent.
403
415
404
416
The ` --no-bin-links` argument will prevent npm from creating symlinks for
405
417
any binaries the package might contain.
406
418
419
+ The ` --no-fund` argument will hide the message displayed at the end of each
420
+ install that acknowledges the number of dependencies looking for funding.
421
+ See [` npm-fund` ](/cli-commands/npm-fund)
422
+
407
423
The ` --no-optional` argument will prevent optional dependencies from
408
424
being installed.
409
425
410
426
The ` --no-shrinkwrap` argument, which will ignore an available
411
- package lock or shrinkwrap file and use the package.json instead.
427
+ package- lock or shrinkwrap file and use the package.json instead.
412
428
413
429
The ` --no-package-lock` argument will prevent npm from creating a
414
430
` package-lock.json` file. When running with package-lock' s disabled npm
@@ -418,32 +434,20 @@ The `--nodedir=/path/to/node/source` argument will allow npm to find the
418
434
node source code so that npm can compile native modules.
419
435
420
436
The `--only={prod[uction]|dev[elopment]}` argument will cause either only
421
- `devDependencies` or only non-`devDependencies` to be installed regardless of the `NODE_ENV`.
437
+ `devDependencies` or only non-`devDependencies` to be installed regardless
438
+ of the `NODE_ENV`.
422
439
423
- The `--no-audit` argument can be used to disable sending of audit reports to
424
- the configured registries. See [`npm-audit`](npm-audit) for details on what is sent.
440
+ The `--strict-peer-deps` argument will cause the install to fail on any
441
+ `peerDependencies` conflict, even if it' s possible to use a heuristic to
442
+ provide a least-surprising resolution to a given conflict.
425
443
426
444
See [` config` ](/using-npm/config). Many of the configuration params have some
427
445
effect on installation, since that' s most of what npm does.
428
446
429
447
#### Algorithm
430
448
431
- To install a package, npm uses the following algorithm:
432
- ` ` ` bash
433
- load the existing node_modules tree from disk
434
- clone the tree
435
- fetch the package.json and assorted metadata and add it to the clone
436
- walk the clone and add any missing dependencies
437
- dependencies will be added as close to the top as is possible
438
- without breaking any other modules
439
- compare the original tree with the cloned tree and make a list of
440
- actions to take to convert one to the other
441
- execute all of the actions, deepest first
442
- kinds of actions are install, update, remove and move
443
- ` ` `
444
-
445
- For this ` package{dep}` structure: ` A{B,C}, B{C}, C{D}` ,
446
- this algorithm produces:
449
+ Given a `package{dep}` structure: `A{B,C}, B{C}, C{D}`,
450
+ the npm install algorithm produces:
447
451
448
452
```bash
449
453
A
466
470
+-- D@1
467
471
```
468
472
469
- Because B' s D@1 will be installed in the top level, C now has to install D@2
473
+ Because B' s D@1 will be installed in the top- level, C now has to install D@2
470
474
privately for itself. This algorithm is deterministic, but different trees may
471
475
be produced if two dependencies are requested for installation in a different
472
476
order.
473
477
474
- See [folders](/configuring-npm/folders) for a more detailed description of the specific folder structures that npm creates.
475
-
476
- ### Limitations of npm' s Install Algorithm
477
-
478
- npm will refuse to install any package with an identical name to the
479
- current package. This can be overridden with the ` --force` flag, but in
480
- most cases can simply be addressed by changing the local package name.
481
-
482
- There are some very rare and pathological edge-cases where a cycle can
483
- cause npm to try to install a never-ending tree of packages. Here is
484
- the simplest case:
485
-
486
- ` ` ` bash
487
- A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...
488
- ` ` `
489
-
490
- where ` A` is some version of a package, and ` A' ` is a different version
491
- of the same package. Because `B` depends on a different version of `A`
492
- than the one that is already in the tree, it must install a separate
493
- copy. The same is true of `A' ` , which must install ` B' `. Because `B' `
494
- depends on the original version of ` A` , which has been overridden, the
495
- cycle falls into infinite regress.
496
-
497
- To avoid this situation, npm flat-out refuses to install any
498
- ` name@version` that is already present anywhere in the tree of package
499
- folder ancestors. A more correct, but more complex, solution would be
500
- to symlink the existing version into the new location. If this ever
501
- affects a real use-case, it will be investigated.
478
+ See [folders](/configuring-npm/folders) for a more detailed description of the
479
+ specific folder structures that npm creates.
502
480
503
481
# ## See Also
504
482
0 commit comments