folders.html 14 KB

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