folders.5 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. .TH "FOLDERS" "5" "August 2021" "" ""
  2. .SH "NAME"
  3. \fBfolders\fR \- Folder Structures Used by npm
  4. .SS Description
  5. .P
  6. npm puts various things on your computer\. That's its job\.
  7. .P
  8. This document will tell you what it puts where\.
  9. .SS tl;dr
  10. .RS 0
  11. .IP \(bu 2
  12. Local install (default): puts stuff in \fB\|\./node_modules\fP of the current
  13. package root\.
  14. .IP \(bu 2
  15. Global install (with \fB\-g\fP): puts stuff in /usr/local or wherever node
  16. is installed\.
  17. .IP \(bu 2
  18. Install it \fBlocally\fR if you're going to \fBrequire()\fP it\.
  19. .IP \(bu 2
  20. Install it \fBglobally\fR if you're going to run it on the command line\.
  21. .IP \(bu 2
  22. If you need both, then install it in both places, or use \fBnpm link\fP\|\.
  23. .RE
  24. .SS prefix Configuration
  25. .P
  26. The \fBprefix\fP config defaults to the location where node is installed\.
  27. On most systems, this is \fB/usr/local\fP\|\. On Windows, it's \fB%AppData%\\npm\fP\|\.
  28. On Unix systems, it's one level up, since node is typically installed at
  29. \fB{prefix}/bin/node\fP rather than \fB{prefix}/node\.exe\fP\|\.
  30. .P
  31. When the \fBglobal\fP flag is set, npm installs things into this prefix\.
  32. When it is not set, it uses the root of the current package, or the
  33. current working directory if not in a package already\.
  34. .SS Node Modules
  35. .P
  36. Packages are dropped into the \fBnode_modules\fP folder under the \fBprefix\fP\|\.
  37. When installing locally, this means that you can
  38. \fBrequire("packagename")\fP to load its main module, or
  39. \fBrequire("packagename/lib/path/to/sub/module")\fP to load other modules\.
  40. .P
  41. Global installs on Unix systems go to \fB{prefix}/lib/node_modules\fP\|\.
  42. Global installs on Windows go to \fB{prefix}/node_modules\fP (that is, no
  43. \fBlib\fP folder\.)
  44. .P
  45. Scoped packages are installed the same way, except they are grouped together
  46. in a sub\-folder of the relevant \fBnode_modules\fP folder with the name of that
  47. scope prefix by the @ symbol, e\.g\. \fBnpm install @myorg/package\fP would place
  48. the package in \fB{prefix}/node_modules/@myorg/package\fP\|\. See npm help \fBscope\fP for more details\.
  49. .P
  50. If you wish to \fBrequire()\fP a package, then install it locally\.
  51. .SS Executables
  52. .P
  53. When in global mode, executables are linked into \fB{prefix}/bin\fP on Unix,
  54. or directly into \fB{prefix}\fP on Windows\.
  55. .P
  56. When in local mode, executables are linked into
  57. \fB\|\./node_modules/\.bin\fP so that they can be made available to scripts run
  58. through npm\. (For example, so that a test runner will be in the path
  59. when you run \fBnpm test\fP\|\.)
  60. .SS Man Pages
  61. .P
  62. When in global mode, man pages are linked into \fB{prefix}/share/man\fP\|\.
  63. .P
  64. When in local mode, man pages are not installed\.
  65. .P
  66. Man pages are not installed on Windows systems\.
  67. .SS Cache
  68. .P
  69. See npm help \fBcache\fP\|\. Cache files are stored in \fB~/\.npm\fP on Posix, or
  70. \fB%AppData%/npm\-cache\fP on Windows\.
  71. .P
  72. This is controlled by the \fBcache\fP configuration param\.
  73. .SS Temp Files
  74. .P
  75. Temporary files are stored by default in the folder specified by the
  76. \fBtmp\fP config, which defaults to the TMPDIR, TMP, or TEMP environment
  77. variables, or \fB/tmp\fP on Unix and \fBc:\\windows\\temp\fP on Windows\.
  78. .P
  79. Temp files are given a unique folder under this root for each run of the
  80. program, and are deleted upon successful exit\.
  81. .SS More Information
  82. .P
  83. When installing locally, npm first tries to find an appropriate
  84. \fBprefix\fP folder\. This is so that \fBnpm install foo@1\.2\.3\fP will install
  85. to the sensible root of your package, even if you happen to have \fBcd\fPed
  86. into some other folder\.
  87. .P
  88. Starting at the $PWD, npm will walk up the folder tree checking for a
  89. folder that contains either a \fBpackage\.json\fP file, or a \fBnode_modules\fP
  90. folder\. If such a thing is found, then that is treated as the effective
  91. "current directory" for the purpose of running npm commands\. (This
  92. behavior is inspired by and similar to git's \.git\-folder seeking
  93. logic when running git commands in a working dir\.)
  94. .P
  95. If no package root is found, then the current folder is used\.
  96. .P
  97. When you run \fBnpm install foo@1\.2\.3\fP, then the package is loaded into
  98. the cache, and then unpacked into \fB\|\./node_modules/foo\fP\|\. Then, any of
  99. foo's dependencies are similarly unpacked into
  100. \fB\|\./node_modules/foo/node_modules/\.\.\.\fP\|\.
  101. .P
  102. Any bin files are symlinked to \fB\|\./node_modules/\.bin/\fP, so that they may
  103. be found by npm scripts when necessary\.
  104. .SS Global Installation
  105. .P
  106. If the \fBglobal\fP configuration is set to true, then npm will
  107. install packages "globally"\.
  108. .P
  109. For global installation, packages are installed roughly the same way,
  110. but using the folders described above\.
  111. .SS Cycles, Conflicts, and Folder Parsimony
  112. .P
  113. Cycles are handled using the property of node's module system that it
  114. walks up the directories looking for \fBnode_modules\fP folders\. So, at every
  115. stage, if a package is already installed in an ancestor \fBnode_modules\fP
  116. folder, then it is not installed at the current location\.
  117. .P
  118. Consider the case above, where \fBfoo \-> bar \-> baz\fP\|\. Imagine if, in
  119. addition to that, baz depended on bar, so you'd have:
  120. \fBfoo \-> bar \-> baz \-> bar \-> baz \.\.\.\fP\|\. However, since the folder
  121. structure is: \fBfoo/node_modules/bar/node_modules/baz\fP, there's no need to
  122. put another copy of bar into \fB\|\.\.\./baz/node_modules\fP, since when it calls
  123. require("bar"), it will get the copy that is installed in
  124. \fBfoo/node_modules/bar\fP\|\.
  125. .P
  126. This shortcut is only used if the exact same
  127. version would be installed in multiple nested \fBnode_modules\fP folders\. It
  128. is still possible to have \fBa/node_modules/b/node_modules/a\fP if the two
  129. "a" packages are different versions\. However, without repeating the
  130. exact same package multiple times, an infinite regress will always be
  131. prevented\.
  132. .P
  133. Another optimization can be made by installing dependencies at the
  134. highest level possible, below the localized "target" folder\.
  135. .SS Example
  136. .P
  137. Consider this dependency graph:
  138. .P
  139. .RS 2
  140. .nf
  141. foo
  142. +\-\- blerg@1\.2\.5
  143. +\-\- bar@1\.2\.3
  144. | +\-\- blerg@1\.x (latest=1\.3\.7)
  145. | +\-\- baz@2\.x
  146. | | `\-\- quux@3\.x
  147. | | `\-\- bar@1\.2\.3 (cycle)
  148. | `\-\- asdf@*
  149. `\-\- baz@1\.2\.3
  150. `\-\- quux@3\.x
  151. `\-\- bar
  152. .fi
  153. .RE
  154. .P
  155. In this case, we might expect a folder structure like this:
  156. .P
  157. .RS 2
  158. .nf
  159. foo
  160. +\-\- node_modules
  161. +\-\- blerg (1\.2\.5) <\-\-\-[A]
  162. +\-\- bar (1\.2\.3) <\-\-\-[B]
  163. | `\-\- node_modules
  164. | +\-\- baz (2\.0\.2) <\-\-\-[C]
  165. | | `\-\- node_modules
  166. | | `\-\- quux (3\.2\.0)
  167. | `\-\- asdf (2\.3\.4)
  168. `\-\- baz (1\.2\.3) <\-\-\-[D]
  169. `\-\- node_modules
  170. `\-\- quux (3\.2\.0) <\-\-\-[E]
  171. .fi
  172. .RE
  173. .P
  174. Since foo depends directly on \fBbar@1\.2\.3\fP and \fBbaz@1\.2\.3\fP, those are
  175. installed in foo's \fBnode_modules\fP folder\.
  176. .P
  177. Even though the latest copy of blerg is 1\.3\.7, foo has a specific
  178. dependency on version 1\.2\.5\. So, that gets installed at [A]\. Since the
  179. parent installation of blerg satisfies bar's dependency on \fBblerg@1\.x\fP,
  180. it does not install another copy under [B]\.
  181. .P
  182. Bar [B] also has dependencies on baz and asdf, so those are installed in
  183. bar's \fBnode_modules\fP folder\. Because it depends on \fBbaz@2\.x\fP, it cannot
  184. re\-use the \fBbaz@1\.2\.3\fP installed in the parent \fBnode_modules\fP folder [D],
  185. and must install its own copy [C]\.
  186. .P
  187. Underneath bar, the \fBbaz \-> quux \-> bar\fP dependency creates a cycle\.
  188. However, because bar is already in quux's ancestry [B], it does not
  189. unpack another copy of bar into that folder\.
  190. .P
  191. Underneath \fBfoo \-> baz\fP [D], quux's [E] folder tree is empty, because its
  192. dependency on bar is satisfied by the parent folder copy installed at [B]\.
  193. .P
  194. For a graphical breakdown of what is installed where, use \fBnpm ls\fP\|\.
  195. .SS Publishing
  196. .P
  197. Upon publishing, npm will look in the \fBnode_modules\fP folder\. If any of
  198. the items there are not in the \fBbundledDependencies\fP array, then they will
  199. not be included in the package tarball\.
  200. .P
  201. This allows a package maintainer to install all of their dependencies
  202. (and dev dependencies) locally, but only re\-publish those items that
  203. cannot be found elsewhere\. See npm help \fBpackage\.json\fP for more information\.
  204. .SS See also
  205. .RS 0
  206. .IP \(bu 2
  207. npm help package\.json
  208. .IP \(bu 2
  209. npm help install
  210. .IP \(bu 2
  211. npm help pack
  212. .IP \(bu 2
  213. npm help cache
  214. .IP \(bu 2
  215. npm help config
  216. .IP \(bu 2
  217. npm help npmrc
  218. .IP \(bu 2
  219. npm help config
  220. .IP \(bu 2
  221. npm help publish
  222. .RE