folders.5 8.3 KB

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