1e5c638e91284938f0853094a79e1e6eb5e06a0c
[platform/upstream/nodejs.git] / deps / npm / html / doc / files / npm-global.html
1 <!doctype html>
2 <html>
3   <title>npm-folders</title>
4   <meta http-equiv="content-type" value="text/html;utf-8">
5   <link rel="stylesheet" type="text/css" href="../../static/style.css">
6
7   <body>
8     <div id="wrapper">
9 <h1><a href="../files/npm-folders.html">npm-folders</a></h1> <p>Folder Structures Used by npm</p>
10
11 <h2 id="DESCRIPTION">DESCRIPTION</h2>
12
13 <p>npm puts various things on your computer.  That&#39;s its job.</p>
14
15 <p>This document will tell you what it puts where.</p>
16
17 <h3 id="tl-dr">tl;dr</h3>
18
19 <ul><li>Local install (default): puts stuff in <code>./node_modules</code> of the current
20 package root.</li><li>Global install (with <code>-g</code>): puts stuff in /usr/local or wherever node
21 is installed.</li><li>Install it <strong>locally</strong> if you&#39;re going to <code>require()</code> it.</li><li>Install it <strong>globally</strong> if you&#39;re going to run it on the command line.</li><li>If you need both, then install it in both places, or use <code>npm link</code>.</li></ul>
22
23 <h3 id="prefix-Configuration">prefix Configuration</h3>
24
25 <p>The <code>prefix</code> config defaults to the location where node is installed.
26 On most systems, this is <code>/usr/local</code>, and most of the time is the same
27 as node&#39;s <code>process.installPrefix</code>.</p>
28
29 <p>On windows, this is the exact location of the node.exe binary.  On Unix
30 systems, it&#39;s one level up, since node is typically installed at
31 <code>{prefix}/bin/node</code> rather than <code>{prefix}/node.exe</code>.</p>
32
33 <p>When the <code>global</code> flag is set, npm installs things into this prefix.
34 When it is not set, it uses the root of the current package, or the
35 current working directory if not in a package already.</p>
36
37 <h3 id="Node-Modules">Node Modules</h3>
38
39 <p>Packages are dropped into the <code>node_modules</code> folder under the <code>prefix</code>.
40 When installing locally, this means that you can
41 <code>require(&quot;packagename&quot;)</code> to load its main module, or
42 <code>require(&quot;packagename/lib/path/to/sub/module&quot;)</code> to load other modules.</p>
43
44 <p>Global installs on Unix systems go to <code>{prefix}/lib/node_modules</code>.
45 Global installs on Windows go to <code>{prefix}/node_modules</code> (that is, no
46 <code>lib</code> folder.)</p>
47
48 <p>If you wish to <code>require()</code> a package, then install it locally.</p>
49
50 <h3 id="Executables">Executables</h3>
51
52 <p>When in global mode, executables are linked into <code>{prefix}/bin</code> on Unix,
53 or directly into <code>{prefix}</code> on Windows.</p>
54
55 <p>When in local mode, executables are linked into
56 <code>./node_modules/.bin</code> so that they can be made available to scripts run
57 through npm.  (For example, so that a test runner will be in the path
58 when you run <code>npm test</code>.)</p>
59
60 <h3 id="Man-Pages">Man Pages</h3>
61
62 <p>When in global mode, man pages are linked into <code>{prefix}/share/man</code>.</p>
63
64 <p>When in local mode, man pages are not installed.</p>
65
66 <p>Man pages are not installed on Windows systems.</p>
67
68 <h3 id="Cache">Cache</h3>
69
70 <p>See <code><a href="../cli/npm-cache.html">npm-cache(1)</a></code>.  Cache files are stored in <code>~/.npm</code> on Posix, or
71 <code>~/npm-cache</code> on Windows.</p>
72
73 <p>This is controlled by the <code>cache</code> configuration param.</p>
74
75 <h3 id="Temp-Files">Temp Files</h3>
76
77 <p>Temporary files are stored by default in the folder specified by the
78 <code>tmp</code> config, which defaults to the TMPDIR, TMP, or TEMP environment
79 variables, or <code>/tmp</code> on Unix and <code>c:\windows\temp</code> on Windows.</p>
80
81 <p>Temp files are given a unique folder under this root for each run of the
82 program, and are deleted upon successful exit.</p>
83
84 <h2 id="More-Information">More Information</h2>
85
86 <p>When installing locally, npm first tries to find an appropriate
87 <code>prefix</code> folder.  This is so that <code>npm install foo@1.2.3</code> will install
88 to the sensible root of your package, even if you happen to have <code>cd</code>ed
89 into some other folder.</p>
90
91 <p>Starting at the $PWD, npm will walk up the folder tree checking for a
92 folder that contains either a <code>package.json</code> file, or a <code>node_modules</code>
93 folder.  If such a thing is found, then that is treated as the effective
94 &quot;current directory&quot; for the purpose of running npm commands.  (This
95 behavior is inspired by and similar to git&#39;s .git-folder seeking
96 logic when running git commands in a working dir.)</p>
97
98 <p>If no package root is found, then the current folder is used.</p>
99
100 <p>When you run <code>npm install foo@1.2.3</code>, then the package is loaded into
101 the cache, and then unpacked into <code>./node_modules/foo</code>.  Then, any of
102 foo&#39;s dependencies are similarly unpacked into
103 <code>./node_modules/foo/node_modules/...</code>.</p>
104
105 <p>Any bin files are symlinked to <code>./node_modules/.bin/</code>, so that they may
106 be found by npm scripts when necessary.</p>
107
108 <h3 id="Global-Installation">Global Installation</h3>
109
110 <p>If the <code>global</code> configuration is set to true, then npm will
111 install packages &quot;globally&quot;.</p>
112
113 <p>For global installation, packages are installed roughly the same way,
114 but using the folders described above.</p>
115
116 <h3 id="Cycles-Conflicts-and-Folder-Parsimony">Cycles, Conflicts, and Folder Parsimony</h3>
117
118 <p>Cycles are handled using the property of node&#39;s module system that it
119 walks up the directories looking for <code>node_modules</code> folders.  So, at every
120 stage, if a package is already installed in an ancestor <code>node_modules</code>
121 folder, then it is not installed at the current location.</p>
122
123 <p>Consider the case above, where <code>foo -&gt; bar -&gt; baz</code>.  Imagine if, in
124 addition to that, baz depended on bar, so you&#39;d have:
125 <code>foo -&gt; bar -&gt; baz -&gt; bar -&gt; baz ...</code>.  However, since the folder
126 structure is: <code>foo/node_modules/bar/node_modules/baz</code>, there&#39;s no need to
127 put another copy of bar into <code>.../baz/node_modules</code>, since when it calls
128 require(&quot;bar&quot;), it will get the copy that is installed in
129 <code>foo/node_modules/bar</code>.</p>
130
131 <p>This shortcut is only used if the exact same
132 version would be installed in multiple nested <code>node_modules</code> folders.  It
133 is still possible to have <code>a/node_modules/b/node_modules/a</code> if the two
134 &quot;a&quot; packages are different versions.  However, without repeating the
135 exact same package multiple times, an infinite regress will always be
136 prevented.</p>
137
138 <p>Another optimization can be made by installing dependencies at the
139 highest level possible, below the localized &quot;target&quot; folder.</p>
140
141 <h4 id="Example">Example</h4>
142
143 <p>Consider this dependency graph:</p>
144
145 <pre><code>foo
146 +-- blerg@1.2.5
147 +-- bar@1.2.3
148 |   +-- blerg@1.x (latest=1.3.7)
149 |   +-- baz@2.x
150 |   |   `-- quux@3.x
151 |   |       `-- bar@1.2.3 (cycle)
152 |   `-- asdf@*
153 `-- baz@1.2.3
154     `-- quux@3.x
155         `-- bar</code></pre>
156
157 <p>In this case, we might expect a folder structure like this:</p>
158
159 <pre><code>foo
160 +-- node_modules
161     +-- blerg (1.2.5) &lt;---[A]
162     +-- bar (1.2.3) &lt;---[B]
163     |   `-- node_modules
164     |       +-- baz (2.0.2) &lt;---[C]
165     |       |   `-- node_modules
166     |       |       `-- quux (3.2.0)
167     |       `-- asdf (2.3.4)
168     `-- baz (1.2.3) &lt;---[D]
169         `-- node_modules
170             `-- quux (3.2.0) &lt;---[E]</code></pre>
171
172 <p>Since foo depends directly on <code>bar@1.2.3</code> and <code>baz@1.2.3</code>, those are
173 installed in foo&#39;s <code>node_modules</code> folder.</p>
174
175 <p>Even though the latest copy of blerg is 1.3.7, foo has a specific
176 dependency on version 1.2.5.  So, that gets installed at [A].  Since the
177 parent installation of blerg satisfies bar&#39;s dependency on <code>blerg@1.x</code>,
178 it does not install another copy under [B].</p>
179
180 <p>Bar [B] also has dependencies on baz and asdf, so those are installed in
181 bar&#39;s <code>node_modules</code> folder.  Because it depends on <code>baz@2.x</code>, it cannot
182 re-use the <code>baz@1.2.3</code> installed in the parent <code>node_modules</code> folder [D],
183 and must install its own copy [C].</p>
184
185 <p>Underneath bar, the <code>baz -&gt; quux -&gt; bar</code> dependency creates a cycle.
186 However, because bar is already in quux&#39;s ancestry [B], it does not
187 unpack another copy of bar into that folder.</p>
188
189 <p>Underneath <code>foo -&gt; baz</code> [D], quux&#39;s [E] folder tree is empty, because its
190 dependency on bar is satisfied by the parent folder copy installed at [B].</p>
191
192 <p>For a graphical breakdown of what is installed where, use <code>npm ls</code>.</p>
193
194 <h3 id="Publishing">Publishing</h3>
195
196 <p>Upon publishing, npm will look in the <code>node_modules</code> folder.  If any of
197 the items there are not in the <code>bundledDependencies</code> array, then they will
198 not be included in the package tarball.</p>
199
200 <p>This allows a package maintainer to install all of their dependencies
201 (and dev dependencies) locally, but only re-publish those items that
202 cannot be found elsewhere.  See <code><a href="../files/package.json.html">package.json(5)</a></code> for more information.</p>
203
204 <h2 id="SEE-ALSO">SEE ALSO</h2>
205
206 <ul><li><a href="../misc/npm-faq.html">npm-faq(7)</a></li><li><a href="../files/package.json.html">package.json(5)</a></li><li><a href="../cli/npm-install.html">npm-install(1)</a></li><li><a href="../cli/npm-pack.html">npm-pack(1)</a></li><li><a href="../cli/npm-cache.html">npm-cache(1)</a></li><li><a href="../cli/npm-config.html">npm-config(1)</a></li><li><a href="../files/npmrc.html">npmrc(5)</a></li><li><a href="../misc/npm-config.html">npm-config(7)</a></li><li><a href="../cli/npm-publish.html">npm-publish(1)</a></li></ul>
207 </div>
208 <p id="footer">npm-folders &mdash; npm@1.3.15</p>
209 <script>
210 ;(function () {
211 var wrapper = document.getElementById("wrapper")
212 var els = Array.prototype.slice.call(wrapper.getElementsByTagName("*"), 0)
213   .filter(function (el) {
214     return el.parentNode === wrapper
215         && el.tagName.match(/H[1-6]/)
216         && el.id
217   })
218 var l = 2
219   , toc = document.createElement("ul")
220 toc.innerHTML = els.map(function (el) {
221   var i = el.tagName.charAt(1)
222     , out = ""
223   while (i > l) {
224     out += "<ul>"
225     l ++
226   }
227   while (i < l) {
228     out += "</ul>"
229     l --
230   }
231   out += "<li><a href='#" + el.id + "'>" +
232     ( el.innerText || el.text || el.innerHTML)
233     + "</a>"
234   return out
235 }).join("\n")
236 toc.id = "toc"
237 document.body.appendChild(toc)
238 })()
239 </script>