Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
folders.html331 linesDownload Raw Back to configuring-npm
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
116header.title .version {
117    font-size: 0.8em;
118    color: #666666;
119}
120
121footer#edit {
122    border-top: solid 1px #e1e4e8;
123    margin: 3em 0 4em 0;
124    padding-top: 2em;
125}
126
127table {
128    width: 100%;
129    margin: 1em 0;
130    border-radius: 6px;
131    border: 1px solid #e1e4e8;
132    overflow: hidden;
133    border-collapse: separate;
134    border-spacing: 0;
135}
136
137table thead {
138    background-color: #f6f8fa;
139}
140
141table tbody {
142    background-color: #ffffff;
143}
144
145table th,
146table td {
147    padding: 0.75em;
148    text-align: left;
149    border-right: 1px solid #e1e4e8;
150    border-bottom: 1px solid #e1e4e8;
151}
152
153table th:last-child,
154table td:last-child {
155    border-right: none;
156}
157
158table tbody tr:last-child td {
159    border-bottom: none;
160}
161
162table th {
163    font-weight: 600;
164    background-color: #f6f8fa;
165}
166
167table code {
168    white-space: nowrap;
169}
170
171</style>
172</head>
173<body>
174<div id="banner">
175<div id="rainbar"></div>
176<div id="logobar">
177<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
178<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
179<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
180</svg>
181<div class="title">
182npm command-line interface
183</div>
184</div>
185</div>
186
187<section id="content">
188<header class="title">
189<h1 id="----folders----11121">
190    <span>Folders</span>
191    <span class="version">@11.12.1</span>
192</h1>
193<span class="description">Folder structures used by npm</span>
194</header>
195
196<section id="table_of_contents">
197<h2 id="table-of-contents">Table of contents</h2>
198<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></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>
199</section>
200
201<div id="_content"><h3 id="description">Description</h3>
202<p>npm puts various things on your computer.
203That's its job.</p>
204<p>This document will tell you what it puts where.</p>
205<h4 id="tldr">tl;dr</h4>
206<ul>
207<li>Local install (default): puts stuff in <code>./node_modules</code> of the current package root.</li>
208<li>Global install (with <code>-g</code>): puts stuff in /usr/local or wherever node is installed.</li>
209<li>Install it <strong>locally</strong> if you're going to <code>require()</code> it.</li>
210<li>Install it <strong>globally</strong> if you're going to run it on the command line.</li>
211<li>If you need both, then install it in both places, or use <code>npm link</code>.</li>
212</ul>
213<h4 id="prefix-configuration">prefix Configuration</h4>
214<p>The <a href="../using-npm/config#prefix.html"><code>prefix</code> config</a> defaults to the location where node is installed.
215On most systems, this is <code>/usr/local</code>.
216On Windows, it's <code>%AppData%\npm</code>.
217On Unix systems, it's one level up, since node is typically installed at <code>{prefix}/bin/node</code> rather than <code>{prefix}/node.exe</code>.</p>
218<p>When the <code>global</code> flag is set, npm installs things into this prefix.
219When it is not set, it uses the root of the current package, or the current working directory if not in a package already.</p>
220<h4 id="node-modules">Node Modules</h4>
221<p>Packages are dropped into the <code>node_modules</code> folder under the <code>prefix</code>.
222When installing locally, this means that you can <code>require("packagename")</code> to load its main module, or <code>require("packagename/lib/path/to/sub/module")</code> to load other modules.</p>
223<p>Global installs on Unix systems go to <code>{prefix}/lib/node_modules</code>.
224Global installs on Windows go to <code>{prefix}/node_modules</code> (that is, no <code>lib</code> folder.)</p>
225<p>Scoped packages are installed the same way, except they are grouped together in a sub-folder of the relevant <code>node_modules</code> folder with the name of that scope prefix by the @ symbol, e.g. <code>npm install @myorg/package</code> would place the package in <code>{prefix}/node_modules/@myorg/package</code>.
226See <a href="../using-npm/scope.html"><code>scope</code></a> for more details.</p>
227<p>If you wish to <code>require()</code> a package, then install it locally.</p>
228<h4 id="executables">Executables</h4>
229<p>When in global mode, executables are linked into <code>{prefix}/bin</code> on Unix, or directly into <code>{prefix}</code> on Windows.
230Ensure that path is in your terminal's <code>PATH</code> environment to run them.</p>
231<p>When in local mode, executables are linked into <code>./node_modules/.bin</code> so that they can be made available to scripts run through npm.
232(For example, so that a test runner will be in the path when you run <code>npm test</code>.)</p>
233<h4 id="man-pages">Man Pages</h4>
234<p>When in global mode, man pages are linked into <code>{prefix}/share/man</code>.</p>
235<p>When in local mode, man pages are not installed.</p>
236<p>Man pages are not installed on Windows systems.</p>
237<h4 id="cache">Cache</h4>
238<p>See <a href="../commands/npm-cache.html"><code>npm cache</code></a>.
239Cache files are stored in <code>~/.npm</code> on Posix, or <code>%LocalAppData%/npm-cache</code> on Windows.</p>
240<p>This is controlled by the <a href="../using-npm/config#cache.html"><code>cache</code> config</a> param.</p>
241<h3 id="more-information">More Information</h3>
242<p>When installing locally, npm first tries to find an appropriate <code>prefix</code> folder.
243This is so that <code>npm install foo@1.2.3</code> will install to the sensible root of your package, even if you happen to have <code>cd</code>ed into some other folder.</p>
244<p>Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a <code>package.json</code> file, or a <code>node_modules</code> folder.
245If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands.
246(This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)</p>
247<p>If no package root is found, then the current folder is used.</p>
248<p>When you run <code>npm install foo@1.2.3</code>, then the package is loaded into the cache, and then unpacked into <code>./node_modules/foo</code>.
249Then, any of foo's dependencies are similarly unpacked into <code>./node_modules/foo/node_modules/...</code>.</p>
250<p>Any bin files are symlinked to <code>./node_modules/.bin/</code>, so that they may be found by npm scripts when necessary.</p>
251<h4 id="global-installation">Global Installation</h4>
252<p>If the <a href="../using-npm/config#global.html"><code>global</code> config</a> is set to true, then npm will install packages "globally".</p>
253<p>For global installation, packages are installed roughly the same way, but using the folders described above.</p>
254<h4 id="cycles-conflicts-and-folder-parsimony">Cycles, Conflicts, and Folder Parsimony</h4>
255<p>Cycles are handled using the property of node's module system that it walks up the directories looking for <code>node_modules</code> folders.
256So, at every stage, if a package is already installed in an ancestor <code>node_modules</code> folder, then it is not installed at the current location.</p>
257<p>Consider the case above, where <code>foo -&gt; bar -&gt; baz</code>.
258Imagine if, in addition to that, baz depended on bar, so you'd have:
259<code>foo -&gt; bar -&gt; baz -&gt; bar -&gt; baz ...</code>.
260However, since the folder structure is: <code>foo/node_modules/bar/node_modules/baz</code>, there's no need to put another copy of bar into <code>.../baz/node_modules</code>, since when baz calls <code>require("bar")</code>, it will get the copy that is installed in <code>foo/node_modules/bar</code>.</p>
261<p>This shortcut is only used if the exact same version would be installed in multiple nested <code>node_modules</code> folders.
262It is still possible to have <code>a/node_modules/b/node_modules/a</code> if the two "a" packages are different versions.
263However, without repeating the exact same package multiple times, an infinite regress will always be prevented.</p>
264<p>Another optimization can be made by installing dependencies at the highest level possible, below the localized "target" folder (hoisting).
265Since version 3, npm hoists dependencies by default.</p>
266<h4 id="example">Example</h4>
267<p>Consider this dependency graph:</p>
268<pre><code class="language-bash">foo
269+-- blerg@1.2.5
270+-- bar@1.2.3
271|   +-- blerg@1.x (latest=1.3.7)
272|   +-- baz@2.x
273|   |   `-- quux@3.x
274|   |       `-- bar@1.2.3 (cycle)
275|   `-- asdf@*
276`-- baz@1.2.3
277    `-- quux@3.x
278        `-- bar
279</code></pre>
280<p>In this case, we might expect a folder structure like this (with all dependencies hoisted to the highest level possible):</p>
281<pre><code class="language-bash">foo
282+-- node_modules
283    +-- blerg (1.2.5) &lt;---[A]
284    +-- bar (1.2.3) &lt;---[B]
285    |   +-- node_modules
286    |       +-- baz (2.0.2) &lt;---[C]
287    +-- asdf (2.3.4)
288    +-- baz (1.2.3) &lt;---[D]
289    +-- quux (3.2.0) &lt;---[E]
290</code></pre>
291<p>Since foo depends directly on <code>bar@1.2.3</code> and <code>baz@1.2.3</code>, those are installed in foo's <code>node_modules</code> folder.</p>
292<p>Even though the latest copy of blerg is 1.3.7, foo has a specific dependency on version 1.2.5.
293So, that gets installed at [A].
294Since the parent installation of blerg satisfies bar's dependency on <code>blerg@1.x</code>, it does not install another copy under [B].</p>
295<p>Bar [B] also has dependencies on baz and asdf.
296Because it depends on <code>baz@2.x</code>, it cannot re-use the <code>baz@1.2.3</code> installed in the parent <code>node_modules</code> folder [D], and must install its own copy [C].
297In order to minimize duplication, npm hoists dependencies to the top level by default, so asdf is installed under [A].</p>
298<p>Underneath bar, the <code>baz -&gt; quux -&gt; bar</code> dependency creates a cycle.
299However, because bar is already in quux's ancestry [B], it does not unpack another copy of bar into that folder.
300Likewise, quux's [E] folder tree is empty, because its dependency on bar is satisfied by the parent folder copy installed at [B].</p>
301<p>For a graphical breakdown of what is installed where, use <code>npm ls</code>.</p>
302<h4 id="publishing">Publishing</h4>
303<p>Upon publishing, npm will look in the <code>node_modules</code> folder.
304If any of the items there are not in the <code>bundleDependencies</code> array, then they will not be included in the package tarball.</p>
305<p>This allows a package maintainer to install all of their dependencies (and dev dependencies) locally, but only re-publish those items that cannot be found elsewhere.
306See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for more information.</p>
307<h3 id="see-also">See also</h3>
308<ul>
309<li><a href="../configuring-npm/package-json.html">package.json</a></li>
310<li><a href="../commands/npm-install.html">npm install</a></li>
311<li><a href="../commands/npm-pack.html">npm pack</a></li>
312<li><a href="../commands/npm-cache.html">npm cache</a></li>
313<li><a href="../commands/npm-config.html">npm config</a></li>
314<li><a href="../configuring-npm/npmrc.html">npmrc</a></li>
315<li><a href="../using-npm/config.html">config</a></li>
316<li><a href="../commands/npm-publish.html">npm publish</a></li>
317</ul></div>
318
319<footer id="edit">
320<a href="https://github.com/npm/cli/edit/latest/docs/lib/content/configuring-npm/folders.md">
321<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
322<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>
323</svg>
324Edit this page on GitHub
325</a>
326</footer>
327</section>
328
329
330
331</body></html>
codekingpro/portable-devtools · Team Ai