folders.html 14 KB

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