3 <title>npm-install</title>
4 <meta http-equiv="content-type" value="text/html;utf-8">
5 <link rel="stylesheet" type="text/css" href="../../static/style.css">
9 <h1><a href="../cli/npm-install.html">npm-install</a></h1> <p>Install a package</p>
11 <h2 id="SYNOPSIS">SYNOPSIS</h2>
13 <pre><code>npm install (with no args in a package dir)
14 npm install <tarball file>
15 npm install <tarball url>
16 npm install <folder>
17 npm install <name> [--save|--save-dev|--save-optional]
18 npm install <name>@<tag>
19 npm install <name>@<version>
20 npm install <name>@<version range>
21 npm i (with any of the previous argument usage)</code></pre>
23 <h2 id="DESCRIPTION">DESCRIPTION</h2>
25 <p>This command installs a package, and any packages that it depends on. If the
26 package has a shrinkwrap file, the installation of dependencies will be driven
27 by that. See <a href="../cli/npm-shrinkwrap.html">npm-shrinkwrap(1)</a>.</p>
29 <p>A <code>package</code> is:</p>
31 <ul><li>a) a folder containing a program described by a package.json file</li><li>b) a gzipped tarball containing (a)</li><li>c) a url that resolves to (b)</li><li>d) a <code><name>@<version></code> that is published on the registry with (c)</li><li>e) a <code><name>@<tag></code> that points to (d)</li><li>f) a <code><name></code> that has a "latest" tag satisfying (e)</li><li>g) a <code><git remote url></code> that resolves to (b)</li></ul>
33 <p>Even if you never publish your package, you can still get a lot of
34 benefits of using npm if you just want to write a node program (a), and
35 perhaps if you also want to be able to easily install it elsewhere
36 after packing it up into a tarball (b).</p>
38 <ul><li><p><code>npm install</code> (in package directory, no arguments):</p><p>Install the dependencies in the local node_modules folder.</p><p>In global mode (ie, with <code>-g</code> or <code>--global</code> appended to the command),
39 it installs the current package context (ie, the current working
40 directory) as a global package.</p><p>By default, <code>npm install</code> will install all modules listed as
41 dependencies. With the <code>--production</code> flag,
42 npm will not install modules listed in <code>devDependencies</code>.</p></li><li><p><code>npm install <folder></code>:</p><p>Install a package that is sitting in a folder on the filesystem.</p></li><li><p><code>npm install <tarball file></code>:</p><p>Install a package that is sitting on the filesystem. Note: if you just want
43 to link a dev directory into your npm root, you can do this more easily by
44 using <code>npm link</code>.</p><p>Example:</p><pre><code> npm install ./package.tgz</code></pre></li><li><p><code>npm install <tarball url></code>:</p><p>Fetch the tarball url, and then install it. In order to distinguish between
45 this and other options, the argument must start with "http://" or "https://"</p><p>Example:</p><pre><code> npm install https://github.com/indexzero/forever/tarball/v0.5.6</code></pre></li><li><p><code>npm install <name> [--save|--save-dev|--save-optional]</code>:</p><p>Do a <code><name>@<tag></code> install, where <code><tag></code> is the "tag" config. (See
46 <code><a href="../misc/npm-config.html">npm-config(7)</a></code>.)</p><p>In most cases, this will install the latest version
47 of the module published on npm.</p><p>Example:</p><p> npm install sax</p><p><code>npm install</code> takes 3 exclusive, optional flags which save or update
48 the package version in your main package.json:</p><ul><li><p><code>--save</code>: Package will appear in your <code>dependencies</code>.</p></li><li><p><code>--save-dev</code>: Package will appear in your <code>devDependencies</code>.</p></li><li><p><code>--save-optional</code>: Package will appear in your <code>optionalDependencies</code>.</p><p>Examples:</p><p> npm install sax --save
49 npm install node-tap --save-dev
50 npm install dtrace-provider --save-optional</p><p><strong>Note</strong>: If there is a file or folder named <code><name></code> in the current
51 working directory, then it will try to install that, and only try to
52 fetch the package by name if it is not valid.</p></li></ul></li><li><p><code>npm install <name>@<tag></code>:</p><p>Install the version of the package that is referenced by the specified tag.
53 If the tag does not exist in the registry data for that package, then this
54 will fail.</p><p>Example:</p><pre><code> npm install sax@latest</code></pre></li><li><p><code>npm install <name>@<version></code>:</p><p>Install the specified version of the package. This will fail if the version
55 has not been published to the registry.</p><p>Example:</p><pre><code> npm install sax@0.1.1</code></pre></li><li><p><code>npm install <name>@<version range></code>:</p><p>Install a version of the package matching the specified version range. This
56 will follow the same rules for resolving dependencies described in <code><a href="../files/package.json.html">package.json(5)</a></code>.</p><p>Note that most version ranges must be put in quotes so that your shell will
57 treat it as a single argument.</p><p>Example:</p><p> npm install sax@">=0.1.0 <0.2.0"</p></li><li><p><code>npm install <git remote url></code>:</p><p>Install a package by cloning a git remote url. The format of the git
58 url is:</p><p> <protocol>://[<user>@]<hostname><separator><path>[#<commit-ish>]</p><p><code><protocol></code> is one of <code>git</code>, <code>git+ssh</code>, <code>git+http</code>, or
59 <code>git+https</code>. If no <code><commit-ish></code> is specified, then <code>master</code> is
60 used.</p><p>Examples:</p><pre><code> git+ssh://git@github.com:isaacs/npm.git#v1.0.27
61 git+https://isaacs@github.com/isaacs/npm.git
62 git://github.com/isaacs/npm.git#v1.0.27</code></pre></li></ul>
64 <p>You may combine multiple arguments, and even multiple types of arguments.
67 <pre><code>npm install sax@">=0.1.0 <0.2.0" bench supervisor</code></pre>
69 <p>The <code>--tag</code> argument will apply to all of the specified install targets. If a
70 tag with the given name exists, the tagged version is preferred over newer
73 <p>The <code>--force</code> argument will force npm to fetch remote resources even if a
74 local copy exists on disk.</p>
76 <pre><code>npm install sax --force</code></pre>
78 <p>The <code>--global</code> argument will cause npm to install the package globally
79 rather than locally. See <code><a href="../files/npm-folders.html">npm-folders(5)</a></code>.</p>
81 <p>The <code>--link</code> argument will cause npm to link global installs into the
82 local space in some cases.</p>
84 <p>The <code>--no-bin-links</code> argument will prevent npm from creating symlinks for
85 any binaries the package might contain.</p>
87 <p>The <code>--no-optional</code> argument will prevent optional dependencies from
90 <p>The <code>--no-shrinkwrap</code> argument, which will ignore an available
91 shrinkwrap file and use the package.json instead.</p>
93 <p>The <code>--nodedir=/path/to/node/source</code> argument will allow npm to find the
94 node source code so that npm can compile native modules.</p>
96 <p>See <code><a href="../misc/npm-config.html">npm-config(7)</a></code>. Many of the configuration params have some
97 effect on installation, since that's most of what npm does.</p>
99 <h2 id="ALGORITHM">ALGORITHM</h2>
101 <p>To install a package, npm uses the following algorithm:</p>
103 <pre><code>install(where, what, family, ancestors)
104 fetch what, unpack to <where>/node_modules/<what>
105 for each dep in what.dependencies
106 resolve dep to precise version
107 for each dep@version in what.dependencies
108 not in <where>/node_modules/<what>/node_modules/*
109 and not in <family>
110 add precise version deps to <family>
111 install(<where>/node_modules/<what>, dep, family)</code></pre>
113 <p>For this <code>package{dep}</code> structure: <code>A{B,C}, B{C}, C{D}</code>,
114 this algorithm produces:</p>
121 <p>That is, the dependency from B to C is satisfied by the fact that A
122 already caused C to be installed at a higher level.</p>
124 <p>See <a href="../files/npm-folders.html">npm-folders(5)</a> for a more detailed description of the specific
125 folder structures that npm creates.</p>
127 <h3 id="Limitations-of-npm-s-Install-Algorithm">Limitations of npm's Install Algorithm</h3>
129 <p>There are some very rare and pathological edge-cases where a cycle can
130 cause npm to try to install a never-ending tree of packages. Here is
131 the simplest case:</p>
133 <pre><code>A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...</code></pre>
135 <p>where <code>A</code> is some version of a package, and <code>A'</code> is a different version
136 of the same package. Because <code>B</code> depends on a different version of <code>A</code>
137 than the one that is already in the tree, it must install a separate
138 copy. The same is true of <code>A'</code>, which must install <code>B'</code>. Because <code>B'</code>
139 depends on the original version of <code>A</code>, which has been overridden, the
140 cycle falls into infinite regress.</p>
142 <p>To avoid this situation, npm flat-out refuses to install any
143 <code>name@version</code> that is already present anywhere in the tree of package
144 folder ancestors. A more correct, but more complex, solution would be
145 to symlink the existing version into the new location. If this ever
146 affects a real use-case, it will be investigated.</p>
148 <h2 id="SEE-ALSO">SEE ALSO</h2>
150 <ul><li><a href="../files/npm-folders.html">npm-folders(5)</a></li><li><a href="../cli/npm-update.html">npm-update(1)</a></li><li><a href="../cli/npm-link.html">npm-link(1)</a></li><li><a href="../cli/npm-rebuild.html">npm-rebuild(1)</a></li><li><a href="../misc/npm-scripts.html">npm-scripts(7)</a></li><li><a href="../cli/npm-build.html">npm-build(1)</a></li><li><a href="../cli/npm-config.html">npm-config(1)</a></li><li><a href="../misc/npm-config.html">npm-config(7)</a></li><li><a href="../files/npmrc.html">npmrc(5)</a></li><li><a href="../misc/npm-registry.html">npm-registry(7)</a></li><li><a href="../files/npm-folders.html">npm-folders(5)</a></li><li><a href="../cli/npm-tag.html">npm-tag(1)</a></li><li><a href="../cli/npm-rm.html">npm-rm(1)</a></li><li><a href="../cli/npm-shrinkwrap.html">npm-shrinkwrap(1)</a></li></ul>
152 <p id="footer">npm-install — npm@1.3.17</p>
155 var wrapper = document.getElementById("wrapper")
156 var els = Array.prototype.slice.call(wrapper.getElementsByTagName("*"), 0)
157 .filter(function (el) {
158 return el.parentNode === wrapper
159 && el.tagName.match(/H[1-6]/)
163 , toc = document.createElement("ul")
164 toc.innerHTML = els.map(function (el) {
165 var i = el.tagName.charAt(1)
175 out += "<li><a href='#" + el.id + "'>" +
176 ( el.innerText || el.text || el.innerHTML)
181 document.body.appendChild(toc)