1 ---
2 title: npm-install
3 section: 1
4 description: Install a package
5 github_repo: npm/cli
6 github_branch: release/v6
7 github_path: docs/content/commands/npm-install.md
8 redirect_from:
9 - /cli-documentation/v6/cli-commands/install
10 - /cli-documentation/v6/cli-commands/npm-install
11 - /cli-documentation/v6/commands/install
12 - /cli-documentation/v6/commands/npm-install
13 - /cli-documentation/v6/install
14 - /cli-documentation/v6/npm-install
15 - /cli/v6/cli-commands/install
16 - /cli/v6/cli-commands/npm-install
17 - /cli/v6/commands/install
18 - /cli/v6/install
19 - /cli/v6/npm-install
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm install (with no args, in package dir)
26 npm install [<@scope>/]<name>
27 npm install [<@scope>/]<name>@<tag>
28 npm install [<@scope>/]<name>@<version>
29 npm install [<@scope>/]<name>@<version range>
30 npm install <alias>@npm:<name>
31 npm install <git-host>:<git-user>/<repo-name>
32 npm install <git repo url>
33 npm install <tarball file>
34 npm install <tarball url>
35 npm install <folder>
36
37 aliases: npm i, npm add
38 common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]
39 ```
40
41 ### Description
42
43 This command installs a package, and any packages that it depends on. If the package has a package-lock or shrinkwrap file, the installation of dependencies will be driven by that, with an `npm-shrinkwrap.json` taking precedence if both files exist. See [package-lock.json](/cli/v6/configuring-npm/package-lock-json) and [`npm shrinkwrap`](/cli/v6/commands/npm-shrinkwrap).
44
45 A `package` is:
46
47 - a) a folder containing a program described by a [`package.json`](/cli/v6/configuring-npm/package-json) file
48 - b) a gzipped tarball containing (a)
49 - c) a url that resolves to (b)
50 - d) a `<name>@<version>` that is published on the registry (see [`registry`](/cli/v6/using-npm/registry)) with (c)
51 - e) a `<name>@<tag>` (see [`npm dist-tag`](/cli/v6/commands/npm-dist-tag)) that points to (d)
52 - f) a `<name>` that has a "latest" tag satisfying (e)
53 - g) a `<git remote url>` that resolves to (a)
54
55 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).
56
57 - `npm install` (in package directory, no arguments):
58
59 Install the dependencies in the local node_modules folder.
60
61 In global mode (ie, with `-g` or `--global` appended to the command), it installs the current package context (ie, the current working directory) as a global package.
62
63 By default, `npm install` will install all modules listed as dependencies in [`package.json`](/cli/v6/configuring-npm/package-json).
64
65 With the `--production` flag (or when the `NODE_ENV` environment variable is set to `production`), npm will not install modules listed in `devDependencies`. To install all modules listed in both `dependencies` and `devDependencies` when `NODE_ENV` environment variable is set to `production`, you can use `--production=false`.
66
67 > NOTE: The `--production` flag has no particular meaning when adding a dependency to a project.
68
69 - `npm install <folder>`:
70
71 Install the package in the directory as a symlink in the current project. Its dependencies will be installed before it's linked. If `<folder>` sits inside the root of your project, its dependencies may be hoisted to the toplevel `node_modules` as they would for other types of dependencies.
72
73 - `npm install <tarball file>`:
74
75 Install a package that is sitting on the filesystem. Note: if you just want to link a dev directory into your npm root, you can do this more easily by using `npm link`.
76
77 Tarball requirements:
78
79 - The filename _must_ use `.tar`, `.tar.gz`, or `.tgz` as the extension.
80 - The package contents should reside in a subfolder inside the tarball (usually it is called `package/`). npm strips one directory layer when installing the package (an equivalent of `tar x --strip-components=1` is run).
81 - The package must contain a `package.json` file with `name` and `version` properties.
82
83 Example:
84
85 npm install ./package.tgz
86
87 - `npm install <tarball url>`:
88
89 Fetch the tarball url, and then install it. In order to distinguish between this and other options, the argument must start with "http://" or "https://"
90
91 Example:
92
93 npm install https://github.com/indexzero/forever/tarball/v0.5.6
94
95 - `npm install [<@scope>/]<name>`:
96
97 Do a `<name>@<tag>` install, where `<tag>` is the "tag" config. (See [`config`](/cli/v6/using-npm/config). The config's default value is `latest`.)
98
99 In most cases, this will install the version of the modules tagged as `latest` on the npm registry.
100
101 Example:
102
103 npm install sax
104
105 - `npm install <alias>@npm:<name>`:
106
107 Install a package under a custom alias. Allows multiple versions of a same-name package side-by-side, more convenient import names for packages with otherwise long ones and using git forks replacements or forked npm packages as replacements. Aliasing works only on your project and does not rename packages in transitive dependencies. Aliases should follow the naming conventions stated in [`validate-npm-package-name`](https://www.npmjs.com/package/validate-npm-package-name#naming-rules).
108
109 Examples:
110
111 npm install my-react@npm:react
112 npm install jquery2@npm:jquery@2
113 npm install jquery3@npm:jquery@3
114 npm install npa@npm:npm-package-arg
115
116 `npm install` saves any specified packages into `dependencies` by default. Additionally, you can control where and how they get saved with some additional flags:
117
118 - `-P, --save-prod`: Package will appear in your `dependencies`. This is the default unless `-D` or `-O` are present.
119
120 - `-D, --save-dev`: Package will appear in your `devDependencies`.
121
122 - `-O, --save-optional`: Package will appear in your `optionalDependencies`.
123
124 - `--no-save`: Prevents saving to `dependencies`.
125
126 When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:
127
128 - `-E, --save-exact`: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator.
129
130 - `-B, --save-bundle`: Saved dependencies will also be added to your `bundleDependencies` list.
131
132 Further, if you have an `npm-shrinkwrap.json` or `package-lock.json` then it will be updated as well.
133
134 `<scope>` is optional. The package will be downloaded from the registry associated with the specified scope. If no registry is associated with the given scope the default registry is assumed. See [`scope`](/cli/v6/using-npm/scope).
135
136 Note: if you do not include the @-symbol on your scope name, npm will interpret this as a GitHub repository instead, see below. Scopes names must also be followed by a slash.
137
138 Examples:
139
140 ```bash
141 npm install sax
142 npm install githubname/reponame
143 npm install @myorg/privatepackage
144 npm install node-tap --save-dev
145 npm install dtrace-provider --save-optional
146 npm install readable-stream --save-exact
147 npm install ansi-regex --save-bundle
148 ```
149
150 **Note**: If there is a file or folder named `<name>` in the current working directory, then it will try to install that, and only try to fetch the package by name if it is not valid.
151
152 - `npm install [<@scope>/]<name>@<tag>`:
153
154 Install the version of the package that is referenced by the specified tag. If the tag does not exist in the registry data for that package, then this will fail.
155
156 Example:
157
158 ```bash
159 npm install sax@latest
160 npm install @myorg/mypackage@latest
161 ```
162
163 - `npm install [<@scope>/]<name>@<version>`:
164
165 Install the specified version of the package. This will fail if the version has not been published to the registry.
166
167 Example:
168
169 ```bash
170 npm install sax@0.1.1
171 npm install @myorg/privatepackage@1.5.0
172 ```
173
174 - `npm install [<@scope>/]<name>@<version range>`:
175
176 Install a version of the package matching the specified version range. This will follow the same rules for resolving dependencies described in [`package.json`](/cli/v6/configuring-npm/package-json).
177
178 Note that most version ranges must be put in quotes so that your shell will treat it as a single argument.
179
180 Example:
181
182 ```bash
183 npm install sax@">=0.1.0 <0.2.0"
184 npm install @myorg/privatepackage@">=0.1.0 <0.2.0"
185 ```
186
187 - `npm install <git remote url>`:
188
189 Installs the package from the hosted git provider, cloning it with `git`. For a full git remote url, only that URL will be attempted.
190
191 ```bash
192 <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
193 ```
194
195 `<protocol>` is one of `git`, `git+ssh`, `git+http`, `git+https`, or `git+file`.
196
197 If `#<commit-ish>` is provided, it will be used to clone exactly that commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is specified, then the default branch of the repository is used.
198
199 If the repository makes use of submodules, those submodules will be cloned as well.
200
201 If the package being installed contains a `prepare` script, its `dependencies` and `devDependencies` will be installed, and the prepare script will be run, before the package is packaged and installed.
202
203 The following git environment variables are recognized by npm and will be added to the environment when running git:
204
205 - `GIT_ASKPASS`
206 - `GIT_EXEC_PATH`
207 - `GIT_PROXY_COMMAND`
208 - `GIT_SSH`
209 - `GIT_SSH_COMMAND`
210 - `GIT_SSL_CAINFO`
211 - `GIT_SSL_NO_VERIFY`
212
213 See the git man page for details.
214
215 Examples:
216
217 ```bash
218 npm install git+ssh://git@github.com:npm/cli.git#v1.0.27
219 npm install git+ssh://git@github.com:npm/cli#semver:^5.0
220 npm install git+https://isaacs@github.com/npm/cli.git
221 npm install git://github.com/npm/cli.git#v1.0.27
222 GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://git@github.com:npm/cli.git
223 ```
224
225 - `npm install <githubname>/<githubrepo>[#<commit-ish>]`:
226 - `npm install github:<githubname>/<githubrepo>[#<commit-ish>]`:
227
228 Install the package at `https://github.com/githubname/githubrepo` by attempting to clone it using `git`.
229
230 If `#<commit-ish>` is provided, it will be used to clone exactly that commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is specified, then `master` is used.
231
232 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
233
234 Examples:
235
236 ```bash
237 npm install mygithubuser/myproject
238 npm install github:mygithubuser/myproject
239 ```
240
241 - `npm install gist:[<githubname>/]<gistID>[#<commit-ish>|#semver:<semver>]`:
242
243 Install the package at `https://gist.github.com/gistID` by attempting to clone it using `git`. The GitHub username associated with the gist is optional and will not be saved in `package.json`.
244
245 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
246
247 Example:
248
249 ```bash
250 npm install gist:101a11beef
251 ```
252
253 - `npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]`:
254
255 Install the package at `https://bitbucket.org/bitbucketname/bitbucketrepo` by attempting to clone it using `git`.
256
257 If `#<commit-ish>` is provided, it will be used to clone exactly that commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is specified, then `master` is used.
258
259 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
260
261 Example:
262
263 ```bash
264 npm install bitbucket:mybitbucketuser/myproject
265 ```
266
267 - `npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]`:
268
269 Install the package at `https://gitlab.com/gitlabname/gitlabrepo` by attempting to clone it using `git`.
270
271 If `#<commit-ish>` is provided, it will be used to clone exactly that commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is specified, then `master` is used.
272
273 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
274
275 Example:
276
277 ```bash
278 npm install gitlab:mygitlabuser/myproject
279 npm install gitlab:myusr/myproj#semver:^5.0
280 ```
281
282 You may combine multiple arguments, and even multiple types of arguments. For example:
283
284 ```bash
285 npm install sax@">=0.1.0 <0.2.0" bench supervisor
286 ```
287
288 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.
289
290 The `--dry-run` argument will report in the usual way what the install would have done without actually installing anything.
291
292 The `--package-lock-only` argument will only update the `package-lock.json`, instead of checking `node_modules` and downloading dependencies.
293
294 The `-f` or `--force` argument will force npm to fetch remote resources even if a local copy exists on disk.
295
296 ```bash
297 npm install sax --force
298 ```
299
300 The `--no-fund` argument will hide the message displayed at the end of each install that acknowledges the number of dependencies looking for funding. See `npm-fund(1)`
301
302 The `-g` or `--global` argument will cause npm to install the package globally rather than locally. See [folders](/cli/v6/configuring-npm/folders).
303
304 The `--global-style` argument will cause npm to install the package into your local `node_modules` folder with the same layout it uses with the global `node_modules` folder. Only your direct dependencies will show in `node_modules` and everything they depend on will be flattened in their `node_modules` folders. This obviously will eliminate some deduping.
305
306 The `--ignore-scripts` argument will cause npm to not execute any scripts defined in the package.json. See [`scripts`](/cli/v6/using-npm/scripts).
307
308 The `--legacy-bundling` argument will cause npm to install the package such that versions of npm prior to 1.4, such as the one included with node 0.8, can install the package. This eliminates all automatic deduping.
309
310 The `--link` argument will cause npm to link global installs into the local space in some cases.
311
312 The `--no-bin-links` argument will prevent npm from creating symlinks for any binaries the package might contain.
313
314 The `--no-optional` argument will prevent optional dependencies from being installed.
315
316 The `--no-shrinkwrap` argument, which will ignore an available package lock or shrinkwrap file and use the package.json instead.
317
318 The `--no-package-lock` argument will prevent npm from creating a `package-lock.json` file. When running with package-lock's disabled npm will not automatically prune your node modules when installing.
319
320 The `--nodedir=/path/to/node/source` argument will allow npm to find the node source code so that npm can compile native modules.
321
322 The `--only={prod[uction]|dev[elopment]}` argument will cause either only `devDependencies` or only non-`devDependencies` to be installed regardless of the `NODE_ENV`.
323
324 The `--no-audit` argument can be used to disable sending of audit reports to the configured registries. See [`npm-audit`](npm-audit) for details on what is sent.
325
326 See [`config`](/cli/v6/using-npm/config). Many of the configuration params have some effect on installation, since that's most of what npm does.
327
328 #### Algorithm
329
330 To install a package, npm uses the following algorithm:
331
332 ```bash
333 load the existing node_modules tree from disk
334 clone the tree
335 fetch the package.json and assorted metadata and add it to the clone
336 walk the clone and add any missing dependencies
337 dependencies will be added as close to the top as is possible
338 without breaking any other modules
339 compare the original tree with the cloned tree and make a list of
340 actions to take to convert one to the other
341 execute all of the actions, deepest first
342 kinds of actions are install, update, remove and move
343 ```
344
345 For this `package{dep}` structure: `A{B,C}, B{C}, C{D}`, this algorithm produces:
346
347 ```bash
348 A
349 +-- B
350 +-- C
351 +-- D
352 ```
353
354 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.
355
356 For `A{B,C}, B{C,D@1}, C{D@2}`, this algorithm produces:
357
358 ```bash
359 A
360 +-- B
361 +-- C
362 `-- D@2
363 +-- D@1
364 ```
365
366 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.
367
368 See [folders](/cli/v6/configuring-npm/folders) for a more detailed description of the specific folder structures that npm creates.
369
370 ### Limitations of npm's Install Algorithm
371
372 npm will refuse to install any package with an identical name to the current package. This can be overridden with the `--force` flag, but in most cases can simply be addressed by changing the local package name.
373
374 There are some very rare and pathological edge-cases where a cycle can cause npm to try to install a never-ending tree of packages. Here is the simplest case:
375
376 ```bash
377 A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...
378 ```
379
380 where `A` is some version of a package, and `A'` is a different version of the same package. Because `B` depends on a different version of `A` than the one that is already in the tree, it must install a separate copy. The same is true of `A'`, which must install `B'`. Because `B'` depends on the original version of `A`, which has been overridden, the cycle falls into infinite regress.
381
382 To avoid this situation, npm flat-out refuses to install any `name@version` that is already present anywhere in the tree of package folder ancestors. A more correct, but more complex, solution would be to symlink the existing version into the new location. If this ever affects a real use-case, it will be investigated.
383
384 ### See Also
385
386 - [npm folders](/cli/v6/configuring-npm/folders)
387 - [npm update](/cli/v6/commands/npm-update)
388 - [npm audit](/cli/v6/commands/npm-audit)
389 - [npm fund](/cli/v6/commands/npm-fund)
390 - [npm link](/cli/v6/commands/npm-link)
391 - [npm rebuild](/cli/v6/commands/npm-rebuild)
392 - [npm scripts](/cli/v6/using-npm/scripts)
393 - [npm build](/cli/v6/commands/npm-build)
394 - [npm config](/cli/v6/commands/npm-config)
395 - [npmrc](/cli/v6/configuring-npm/npmrc)
396 - [npm registry](/cli/v6/using-npm/registry)
397 - [npm dist-tag](/cli/v6/commands/npm-dist-tag)
398 - [npm uninstall](/cli/v6/commands/npm-uninstall)
399 - [npm shrinkwrap](/cli/v6/commands/npm-shrinkwrap)
400 - [package.json](/cli/v6/configuring-npm/package-json)