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 - The filename _must_ use `.tar`, `.tar.gz`, or `.tgz` as the extension.
79 - 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).
80 - The package must contain a `package.json` file with `name` and `version` properties.
81
82 Example:
83
84 npm install ./package.tgz
85
86 - `npm install <tarball url>`:
87
88 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://"
89
90 Example:
91
92 npm install https://github.com/indexzero/forever/tarball/v0.5.6
93
94 - `npm install [<@scope>/]<name>`:
95
96 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`.)
97
98 In most cases, this will install the version of the modules tagged as `latest` on the npm registry.
99
100 Example:
101
102 npm install sax
103
104 - `npm install <alias>@npm:<name>`:
105
106 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).
107
108 Examples:
109
110 npm install my-react@npm:react
111 npm install jquery2@npm:jquery@2
112 npm install jquery3@npm:jquery@3
113 npm install npa@npm:npm-package-arg
114
115 `npm install` saves any specified packages into `dependencies` by default. Additionally, you can control where and how they get saved with some additional flags:
116 - `-P, --save-prod`: Package will appear in your `dependencies`. This is the default unless `-D` or `-O` are present.
117
118 - `-D, --save-dev`: Package will appear in your `devDependencies`.
119
120 - `-O, --save-optional`: Package will appear in your `optionalDependencies`.
121
122 - `--no-save`: Prevents saving to `dependencies`.
123
124 When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:
125 - `-E, --save-exact`: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator.
126
127 - `-B, --save-bundle`: Saved dependencies will also be added to your `bundleDependencies` list.
128
129 Further, if you have an `npm-shrinkwrap.json` or `package-lock.json` then it will be updated as well.
130
131 `<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).
132
133 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.
134
135 Examples:
136
137 ```bash
138 npm install sax
139 npm install githubname/reponame
140 npm install @myorg/privatepackage
141 npm install node-tap --save-dev
142 npm install dtrace-provider --save-optional
143 npm install readable-stream --save-exact
144 npm install ansi-regex --save-bundle
145 ```
146
147 **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.
148
149 - `npm install [<@scope>/]<name>@<tag>`:
150
151 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.
152
153 Example:
154
155 ```bash
156 npm install sax@latest
157 npm install @myorg/mypackage@latest
158 ```
159
160 - `npm install [<@scope>/]<name>@<version>`:
161
162 Install the specified version of the package. This will fail if the version has not been published to the registry.
163
164 Example:
165
166 ```bash
167 npm install sax@0.1.1
168 npm install @myorg/privatepackage@1.5.0
169 ```
170
171 - `npm install [<@scope>/]<name>@<version range>`:
172
173 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).
174
175 Note that most version ranges must be put in quotes so that your shell will treat it as a single argument.
176
177 Example:
178
179 ```bash
180 npm install sax@">=0.1.0 <0.2.0"
181 npm install @myorg/privatepackage@">=0.1.0 <0.2.0"
182 ```
183
184 - `npm install <git remote url>`:
185
186 Installs the package from the hosted git provider, cloning it with `git`. For a full git remote url, only that URL will be attempted.
187
188 ```bash
189 <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
190 ```
191
192 `<protocol>` is one of `git`, `git+ssh`, `git+http`, `git+https`, or `git+file`.
193
194 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.
195
196 If the repository makes use of submodules, those submodules will be cloned as well.
197
198 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.
199
200 The following git environment variables are recognized by npm and will be added to the environment when running git:
201 - `GIT_ASKPASS`
202 - `GIT_EXEC_PATH`
203 - `GIT_PROXY_COMMAND`
204 - `GIT_SSH`
205 - `GIT_SSH_COMMAND`
206 - `GIT_SSL_CAINFO`
207 - `GIT_SSL_NO_VERIFY`
208
209 See the git man page for details.
210
211 Examples:
212
213 ```bash
214 npm install git+ssh://git@github.com:npm/cli.git#v1.0.27
215 npm install git+ssh://git@github.com:npm/cli#semver:^5.0
216 npm install git+https://isaacs@github.com/npm/cli.git
217 npm install git://github.com/npm/cli.git#v1.0.27
218 GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://git@github.com:npm/cli.git
219 ```
220
221 - `npm install <githubname>/<githubrepo>[#<commit-ish>]`:
222 - `npm install github:<githubname>/<githubrepo>[#<commit-ish>]`:
223
224 Install the package at `https://github.com/githubname/githubrepo` by attempting to clone it using `git`.
225
226 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.
227
228 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
229
230 Examples:
231
232 ```bash
233 npm install mygithubuser/myproject
234 npm install github:mygithubuser/myproject
235 ```
236
237 - `npm install gist:[<githubname>/]<gistID>[#<commit-ish>|#semver:<semver>]`:
238
239 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`.
240
241 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
242
243 Example:
244
245 ```bash
246 npm install gist:101a11beef
247 ```
248
249 - `npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]`:
250
251 Install the package at `https://bitbucket.org/bitbucketname/bitbucketrepo` by attempting to clone it using `git`.
252
253 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.
254
255 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
256
257 Example:
258
259 ```bash
260 npm install bitbucket:mybitbucketuser/myproject
261 ```
262
263 - `npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]`:
264
265 Install the package at `https://gitlab.com/gitlabname/gitlabrepo` by attempting to clone it using `git`.
266
267 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.
268
269 As with regular git dependencies, `dependencies` and `devDependencies` will be installed if the package has a `prepare` script, before the package is done installing.
270
271 Example:
272
273 ```bash
274 npm install gitlab:mygitlabuser/myproject
275 npm install gitlab:myusr/myproj#semver:^5.0
276 ```
277
278 You may combine multiple arguments, and even multiple types of arguments. For example:
279
280 ```bash
281 npm install sax@">=0.1.0 <0.2.0" bench supervisor
282 ```
283
284 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.
285
286 The `--dry-run` argument will report in the usual way what the install would have done without actually installing anything.
287
288 The `--package-lock-only` argument will only update the `package-lock.json`, instead of checking `node_modules` and downloading dependencies.
289
290 The `-f` or `--force` argument will force npm to fetch remote resources even if a local copy exists on disk.
291
292 ```bash
293 npm install sax --force
294 ```
295
296 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)`
297
298 The `-g` or `--global` argument will cause npm to install the package globally rather than locally. See [folders](/cli/v6/configuring-npm/folders).
299
300 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.
301
302 The `--ignore-scripts` argument will cause npm to not execute any scripts defined in the package.json. See [`scripts`](/cli/v6/using-npm/scripts).
303
304 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.
305
306 The `--link` argument will cause npm to link global installs into the local space in some cases.
307
308 The `--no-bin-links` argument will prevent npm from creating symlinks for any binaries the package might contain.
309
310 The `--no-optional` argument will prevent optional dependencies from being installed.
311
312 The `--no-shrinkwrap` argument, which will ignore an available package lock or shrinkwrap file and use the package.json instead.
313
314 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.
315
316 The `--nodedir=/path/to/node/source` argument will allow npm to find the node source code so that npm can compile native modules.
317
318 The `--only={prod[uction]|dev[elopment]}` argument will cause either only `devDependencies` or only non-`devDependencies` to be installed regardless of the `NODE_ENV`.
319
320 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.
321
322 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.
323
324 #### Algorithm
325
326 To install a package, npm uses the following algorithm:
327
328 ```bash
329 load the existing node_modules tree from disk
330 clone the tree
331 fetch the package.json and assorted metadata and add it to the clone
332 walk the clone and add any missing dependencies
333 dependencies will be added as close to the top as is possible
334 without breaking any other modules
335 compare the original tree with the cloned tree and make a list of
336 actions to take to convert one to the other
337 execute all of the actions, deepest first
338 kinds of actions are install, update, remove and move
339 ```
340
341 For this `package{dep}` structure: `A{B,C}, B{C}, C{D}`, this algorithm produces:
342
343 ```bash
344 A
345 +-- B
346 +-- C
347 +-- D
348 ```
349
350 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.
351
352 For `A{B,C}, B{C,D@1}, C{D@2}`, this algorithm produces:
353
354 ```bash
355 A
356 +-- B
357 +-- C
358 `-- D@2
359 +-- D@1
360 ```
361
362 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.
363
364 See [folders](/cli/v6/configuring-npm/folders) for a more detailed description of the specific folder structures that npm creates.
365
366 ### Limitations of npm's Install Algorithm
367
368 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.
369
370 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:
371
372 ```bash
373 A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...
374 ```
375
376 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.
377
378 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.
379
380 ### See Also
381
382 - [npm folders](/cli/v6/configuring-npm/folders)
383 - [npm update](/cli/v6/commands/npm-update)
384 - [npm audit](/cli/v6/commands/npm-audit)
385 - [npm fund](/cli/v6/commands/npm-fund)
386 - [npm link](/cli/v6/commands/npm-link)
387 - [npm rebuild](/cli/v6/commands/npm-rebuild)
388 - [npm scripts](/cli/v6/using-npm/scripts)
389 - [npm build](/cli/v6/commands/npm-build)
390 - [npm config](/cli/v6/commands/npm-config)
391 - [npmrc](/cli/v6/configuring-npm/npmrc)
392 - [npm registry](/cli/v6/using-npm/registry)
393 - [npm dist-tag](/cli/v6/commands/npm-dist-tag)
394 - [npm uninstall](/cli/v6/commands/npm-uninstall)
395 - [npm shrinkwrap](/cli/v6/commands/npm-shrinkwrap)
396 - [package.json](/cli/v6/configuring-npm/package-json)