1<!DOCTYPE html><html><head>
2<meta charset="utf-8">
3<title>folders</title>
4<style>
5body {
6    background-color: #ffffff;
7    color: #24292e;
8
9    margin: 0;
10
11    line-height: 1.5;
12
13    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
14}
15#rainbar {
16    height: 10px;
17    background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
18}
19
20a {
21    text-decoration: none;
22    color: #0366d6;
23}
24a:hover {
25    text-decoration: underline;
26}
27
28pre {
29    margin: 1em 0px;
30    padding: 1em;
31    border: solid 1px #e1e4e8;
32    border-radius: 6px;
33
34    display: block;
35    overflow: auto;
36
37    white-space: pre;
38
39    background-color: #f6f8fa;
40    color: #393a34;
41}
42code {
43    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
44    font-size: 85%;
45    padding: 0.2em 0.4em;
46    background-color: #f6f8fa;
47    color: #393a34;
48}
49pre > code {
50    padding: 0;
51    background-color: inherit;
52    color: inherit;
53}
54h1, h2, h3 {
55    font-weight: 600;
56}
57
58#logobar {
59    background-color: #333333;
60    margin: 0 auto;
61    padding: 1em 4em;
62}
63#logobar .logo {
64    float: left;
65}
66#logobar .title {
67    font-weight: 600;
68    color: #dddddd;
69    float: left;
70    margin: 5px 0 0 1em;
71}
72#logobar:after {
73    content: "";
74    display: block;
75    clear: both;
76}
77
78#content {
79    margin: 0 auto;
80    padding: 0 4em;
81}
82
83#table_of_contents > h2 {
84    font-size: 1.17em;
85}
86#table_of_contents ul:first-child {
87    border: solid 1px #e1e4e8;
88    border-radius: 6px;
89    padding: 1em;
90    background-color: #f6f8fa;
91    color: #393a34;
92}
93#table_of_contents ul {
94    list-style-type: none;
95    padding-left: 1.5em;
96}
97#table_of_contents li {
98    font-size: 0.9em;
99}
100#table_of_contents li a {
101    color: #000000;
102}
103
104header.title {
105    border-bottom: solid 1px #e1e4e8;
106}
107header.title > h1 {
108    margin-bottom: 0.25em;
109}
110header.title > .description {
111    display: block;
112    margin-bottom: 0.5em;
113    line-height: 1;
114}
115
116footer#edit {
117    border-top: solid 1px #e1e4e8;
118    margin: 3em 0 4em 0;
119    padding-top: 2em;
120}
121</style>
122</head>
123<body>
124<div id="banner">
125<div id="rainbar"></div>
126<div id="logobar">
127<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
128<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
129<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
130</svg>
131<div class="title">
132npm command-line interface
133</div>
134</div>
135</div>
136
137<section id="content">
138<header class="title">
139<h1 id="folders">folders</h1>
140<span class="description">Folder Structures Used by npm</span>
141</header>
142
143<section id="table_of_contents">
144<h2 id="table-of-contents">Table of contents</h2>
145<div id="_table_of_contents"><ul><li><a href="#description">Description</a></li><ul><li><a href="#tldr">tl;dr</a></li><li><a href="#prefix-configuration">prefix Configuration</a></li><li><a href="#node-modules">Node Modules</a></li><li><a href="#executables">Executables</a></li><li><a href="#man-pages">Man Pages</a></li><li><a href="#cache">Cache</a></li><li><a href="#temp-files">Temp Files</a></li></ul><li><a href="#more-information">More Information</a></li><ul><li><a href="#global-installation">Global Installation</a></li><li><a href="#cycles-conflicts-and-folder-parsimony">Cycles, Conflicts, and Folder Parsimony</a></li><li><a href="#example">Example</a></li><li><a href="#publishing">Publishing</a></li></ul><li><a href="#see-also">See also</a></li></ul></div>
146</section>
147
148<div id="_content"><h3 id="description">Description</h3>
149<p>npm puts various things on your computer.  That's its job.</p>
150<p>This document will tell you what it puts where.</p>
151<h4 id="tldr">tl;dr</h4>
152<ul>
153<li>Local install (default): puts stuff in <code>./node_modules</code> of the current
154package root.</li>
155<li>Global install (with <code>-g</code>): puts stuff in /usr/local or wherever node
156is installed.</li>
157<li>Install it <strong>locally</strong> if you're going to <code>require()</code> it.</li>
158<li>Install it <strong>globally</strong> if you're going to run it on the command line.</li>
159<li>If you need both, then install it in both places, or use <code>npm link</code>.</li>
160</ul>
161<h4 id="prefix-configuration">prefix Configuration</h4>
162<p>The <a href="../using-npm/config#prefix.html"><code>prefix</code> config</a> defaults to the location where
163node is installed. On most systems, this is <code>/usr/local</code>. On Windows, it's
164<code>%AppData%\npm</code>. On Unix systems, it's one level up, since node is typically
165installed at <code>{prefix}/bin/node</code> rather than <code>{prefix}/node.exe</code>.</p>
166<p>When the <code>global</code> flag is set, npm installs things into this prefix.
167When it is not set, it uses the root of the current package, or the
168current working directory if not in a package already.</p>
169<h4 id="node-modules">Node Modules</h4>
170<p>Packages are dropped into the <code>node_modules</code> folder under the <code>prefix</code>.
171When installing locally, this means that you can
172<code>require("packagename")</code> to load its main module, or
173<code>require("packagename/lib/path/to/sub/module")</code> to load other modules.</p>
174<p>Global installs on Unix systems go to <code>{prefix}/lib/node_modules</code>.
175Global installs on Windows go to <code>{prefix}/node_modules</code> (that is, no
176<code>lib</code> folder.)</p>
177<p>Scoped packages are installed the same way, except they are grouped together
178in a sub-folder of the relevant <code>node_modules</code> folder with the name of that
179scope prefix by the @ symbol, e.g. <code>npm install @myorg/package</code> would place
180the package in <code>{prefix}/node_modules/@myorg/package</code>. See
181<a href="../using-npm/scope.html"><code>scope</code></a> for more details.</p>
182<p>If you wish to <code>require()</code> a package, then install it locally.</p>
183<h4 id="executables">Executables</h4>
184<p>When in global mode, executables are linked into <code>{prefix}/bin</code> on Unix,
185or directly into <code>{prefix}</code> on Windows.  Ensure that path is in your
186terminal's <code>PATH</code> environment to run them.</p>
187<p>When in local mode, executables are linked into
188<code>./node_modules/.bin</code> so that they can be made available to scripts run
189through npm.  (For example, so that a test runner will be in the path
190when you run <code>npm test</code>.)</p>
191<h4 id="man-pages">Man Pages</h4>
192<p>When in global mode, man pages are linked into <code>{prefix}/share/man</code>.</p>
193<p>When in local mode, man pages are not installed.</p>
194<p>Man pages are not installed on Windows systems.</p>
195<h4 id="cache">Cache</h4>
196<p>See <a href="../commands/npm-cache.html"><code>npm cache</code></a>.  Cache files are stored in <code>~/.npm</code> on Posix, or
197<code>%LocalAppData%/npm-cache</code> on Windows.</p>
198<p>This is controlled by the <a href="../using-npm/config#cache.html"><code>cache</code> config</a> param.</p>
199<h4 id="temp-files">Temp Files</h4>
200<p>Temporary files are stored by default in the folder specified by the
201<a href="../using-npm/config#tmp.html"><code>tmp</code> config</a>, which defaults to the TMPDIR, TMP, or
202TEMP environment variables, or <code>/tmp</code> on Unix and <code>c:\windows\temp</code> on Windows.</p>
203<p>Temp files are given a unique folder under this root for each run of the
204program, and are deleted upon successful exit.</p>
205<h3 id="more-information">More Information</h3>
206<p>When installing locally, npm first tries to find an appropriate
207<code>prefix</code> folder.  This is so that <code>npm install foo@1.2.3</code> will install
208to the sensible root of your package, even if you happen to have <code>cd</code>ed
209into some other folder.</p>
210<p>Starting at the $PWD, npm will walk up the folder tree checking for a
211folder that contains either a <code>package.json</code> file, or a <code>node_modules</code>
212folder.  If such a thing is found, then that is treated as the effective
213"current directory" for the purpose of running npm commands.  (This
214behavior is inspired by and similar to git's .git-folder seeking
215logic when running git commands in a working dir.)</p>
216<p>If no package root is found, then the current folder is used.</p>
217<p>When you run <code>npm install foo@1.2.3</code>, then the package is loaded into
218the cache, and then unpacked into <code>./node_modules/foo</code>.  Then, any of
219foo's dependencies are similarly unpacked into
220<code>./node_modules/foo/node_modules/...</code>.</p>
221<p>Any bin files are symlinked to <code>./node_modules/.bin/</code>, so that they may
222be found by npm scripts when necessary.</p>
223<h4 id="global-installation">Global Installation</h4>
224<p>If the <a href="../using-npm/config#global.html"><code>global</code> config</a> is set to true, then npm will
225install packages "globally".</p>
226<p>For global installation, packages are installed roughly the same way,
227but using the folders described above.</p>
228<h4 id="cycles-conflicts-and-folder-parsimony">Cycles, Conflicts, and Folder Parsimony</h4>
229<p>Cycles are handled using the property of node's module system that it
230walks up the directories looking for <code>node_modules</code> folders.  So, at every
231stage, if a package is already installed in an ancestor <code>node_modules</code>
232folder, then it is not installed at the current location.</p>
233<p>Consider the case above, where <code>foo -&gt; bar -&gt; baz</code>.  Imagine if, in
234addition to that, baz depended on bar, so you'd have:
235<code>foo -&gt; bar -&gt; baz -&gt; bar -&gt; baz ...</code>.  However, since the folder
236structure is: <code>foo/node_modules/bar/node_modules/baz</code>, there's no need to
237put another copy of bar into <code>.../baz/node_modules</code>, since when baz calls
238<code>require("bar")</code>, it will get the copy that is installed in
239<code>foo/node_modules/bar</code>.</p>
240<p>This shortcut is only used if the exact same
241version would be installed in multiple nested <code>node_modules</code> folders.  It
242is still possible to have <code>a/node_modules/b/node_modules/a</code> if the two
243"a" packages are different versions.  However, without repeating the
244exact same package multiple times, an infinite regress will always be
245prevented.</p>
246<p>Another optimization can be made by installing dependencies at the
247highest level possible, below the localized "target" folder (hoisting).
248Since version 3, npm hoists dependencies by default.</p>
249<h4 id="example">Example</h4>
250<p>Consider this dependency graph:</p>
251<pre><code class="language-bash">foo
252+-- blerg@1.2.5
253+-- bar@1.2.3
254|   +-- blerg@1.x (latest=1.3.7)
255|   +-- baz@2.x
256|   |   `-- quux@3.x
257|   |       `-- bar@1.2.3 (cycle)
258|   `-- asdf@*
259`-- baz@1.2.3
260    `-- quux@3.x
261        `-- bar
262</code></pre>
263<p>In this case, we might expect a folder structure like this
264(with all dependencies hoisted to the highest level possible):</p>
265<pre><code class="language-bash">foo
266+-- node_modules
267    +-- blerg (1.2.5) &lt;---[A]
268    +-- bar (1.2.3) &lt;---[B]
269    |   +-- node_modules
270    |       +-- baz (2.0.2) &lt;---[C]
271    +-- asdf (2.3.4)
272    +-- baz (1.2.3) &lt;---[D]
273    +-- quux (3.2.0) &lt;---[E]
274</code></pre>
275<p>Since foo depends directly on <code>bar@1.2.3</code> and <code>baz@1.2.3</code>, those are
276installed in foo's <code>node_modules</code> folder.</p>
277<p>Even though the latest copy of blerg is 1.3.7, foo has a specific
278dependency on version 1.2.5.  So, that gets installed at [A].  Since the
279parent installation of blerg satisfies bar's dependency on <code>blerg@1.x</code>,
280it does not install another copy under [B].</p>
281<p>Bar [B] also has dependencies on baz and asdf.  Because it depends on <code>baz@2.x</code>, it cannot
282re-use the <code>baz@1.2.3</code> installed in the parent <code>node_modules</code> folder [D],
283and must install its own copy [C]. In order to minimize duplication, npm hoists
284dependencies to the top level by default, so asdf is installed under [A].</p>
285<p>Underneath bar, the <code>baz -&gt; quux -&gt; bar</code> dependency creates a cycle.
286However, because bar is already in quux's ancestry [B], it does not
287unpack another copy of bar into that folder. Likewise, quux's [E]
288folder tree is empty, because its dependency on bar is satisfied
289by the parent folder copy installed at [B].</p>
290<p>For a graphical breakdown of what is installed where, use <code>npm ls</code>.</p>
291<h4 id="publishing">Publishing</h4>
292<p>Upon publishing, npm will look in the <code>node_modules</code> folder.  If any of
293the items there are not in the <code>bundleDependencies</code> array, then they will
294not be included in the package tarball.</p>
295<p>This allows a package maintainer to install all of their dependencies
296(and dev dependencies) locally, but only re-publish those items that
297cannot be found elsewhere.  See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for more information.</p>
298<h3 id="see-also">See also</h3>
299<ul>
300<li><a href="../configuring-npm/package-json.html">package.json</a></li>
301<li><a href="../commands/npm-install.html">npm install</a></li>
302<li><a href="../commands/npm-pack.html">npm pack</a></li>
303<li><a href="../commands/npm-cache.html">npm cache</a></li>
304<li><a href="../commands/npm-config.html">npm config</a></li>
305<li><a href="../configuring-npm/npmrc.html">npmrc</a></li>
306<li><a href="../using-npm/config.html">config</a></li>
307<li><a href="../commands/npm-publish.html">npm publish</a></li>
308</ul></div>
309
310<footer id="edit">
311<a href="https://github.com/npm/cli/edit/latest/docs/content/configuring-npm/folders.md">
312<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
313<path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
314</svg>
315Edit this page on GitHub
316</a>
317</footer>
318</section>
319
320
321
322</body></html>