package-locks.5 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. .TH "PACKAGE\-LOCKS" "5" "August 2021" "" ""
  2. .SH "NAME"
  3. \fBpackage-locks\fR \- An explanation of npm lockfiles
  4. .SS Description
  5. .P
  6. Conceptually, the "input" to npm help \fBinstall\fP is a npm help package\.json, while its
  7. "output" is a fully\-formed \fBnode_modules\fP tree: a representation of the
  8. dependencies you declared\. In an ideal world, npm would work like a pure
  9. function: the same \fBpackage\.json\fP should produce the exact same \fBnode_modules\fP
  10. tree, any time\. In some cases, this is indeed true\. But in many others, npm is
  11. unable to do this\. There are multiple reasons for this:
  12. .RS 0
  13. .IP \(bu 2
  14. different versions of npm (or other package managers) may have been used to install a package, each using slightly different installation algorithms\.
  15. .IP \(bu 2
  16. a new version of a direct semver\-range package may have been published since the last time your packages were installed, and thus a newer version will be used\.
  17. .IP \(bu 2
  18. A dependency of one of your dependencies may have published a new version, which will update even if you used pinned dependency specifiers (\fB1\.2\.3\fP instead of \fB^1\.2\.3\fP)
  19. .IP \(bu 2
  20. The registry you installed from is no longer available, or allows mutation of versions (unlike the primary npm registry), and a different version of a package exists under the same version number now\.
  21. .RE
  22. .P
  23. As an example, consider package A:
  24. .P
  25. .RS 2
  26. .nf
  27. {
  28. "name": "A",
  29. "version": "0\.1\.0",
  30. "dependencies": {
  31. "B": "<0\.1\.0"
  32. }
  33. }
  34. .fi
  35. .RE
  36. .P
  37. package B:
  38. .P
  39. .RS 2
  40. .nf
  41. {
  42. "name": "B",
  43. "version": "0\.0\.1",
  44. "dependencies": {
  45. "C": "<0\.1\.0"
  46. }
  47. }
  48. .fi
  49. .RE
  50. .P
  51. and package C:
  52. .P
  53. .RS 2
  54. .nf
  55. {
  56. "name": "C",
  57. "version": "0\.0\.1"
  58. }
  59. .fi
  60. .RE
  61. .P
  62. If these are the only versions of A, B, and C available in the
  63. registry, then a normal \fBnpm install A\fP will install:
  64. .P
  65. .RS 2
  66. .nf
  67. A@0\.1\.0
  68. `\-\- B@0\.0\.1
  69. `\-\- C@0\.0\.1
  70. .fi
  71. .RE
  72. .P
  73. However, if B@0\.0\.2 is published, then a fresh \fBnpm install A\fP will
  74. install:
  75. .P
  76. .RS 2
  77. .nf
  78. A@0\.1\.0
  79. `\-\- B@0\.0\.2
  80. `\-\- C@0\.0\.1
  81. .fi
  82. .RE
  83. .P
  84. assuming the new version did not modify B's dependencies\. Of course,
  85. the new version of B could include a new version of C and any number
  86. of new dependencies\. If such changes are undesirable, the author of A
  87. could specify a dependency on B@0\.0\.1\|\. However, if A's author and B's
  88. author are not the same person, there's no way for A's author to say
  89. that he or she does not want to pull in newly published versions of C
  90. when B hasn't changed at all\.
  91. .P
  92. To prevent this potential issue, npm uses npm help package\-lock\.json or, if present, npm help npm\-shrinkwrap\.json\. These files are called package locks, or lockfiles\.
  93. .P
  94. Whenever you run \fBnpm install\fP, npm generates or updates your package lock,
  95. which will look something like this:
  96. .P
  97. .RS 2
  98. .nf
  99. {
  100. "name": "A",
  101. "version": "0\.1\.0",
  102. \.\.\.metadata fields\.\.\.
  103. "dependencies": {
  104. "B": {
  105. "version": "0\.0\.1",
  106. "resolved": "https://registry\.npmjs\.org/B/\-/B\-0\.0\.1\.tgz",
  107. "integrity": "sha512\-DeAdb33F+"
  108. "dependencies": {
  109. "C": {
  110. "version": "git://github\.com/org/C\.git#5c380ae319fc4efe9e7f2d9c78b0faa588fd99b4"
  111. }
  112. }
  113. }
  114. }
  115. }
  116. .fi
  117. .RE
  118. .P
  119. This file describes an \fIexact\fR, and more importantly \fIreproducible\fR
  120. \fBnode_modules\fP tree\. Once it's present, any future installation will base its
  121. work off this file, instead of recalculating dependency versions off
  122. npm help package\.json\.
  123. .P
  124. The presence of a package lock changes the installation behavior such that:
  125. .RS 0
  126. .IP 1. 3
  127. The module tree described by the package lock is reproduced\. This means
  128. reproducing the structure described in the file, using the specific files
  129. referenced in "resolved" if available, falling back to normal package resolution
  130. using "version" if one isn't\.
  131. .IP 2. 3
  132. The tree is walked and any missing dependencies are installed in the usual
  133. fashion\.
  134. .RE
  135. .P
  136. If \fBpreshrinkwrap\fP, \fBshrinkwrap\fP or \fBpostshrinkwrap\fP are in the \fBscripts\fP
  137. property of the \fBpackage\.json\fP, they will be executed in order\. \fBpreshrinkwrap\fP
  138. and \fBshrinkwrap\fP are executed before the shrinkwrap, \fBpostshrinkwrap\fP is
  139. executed afterwards\. These scripts run for both \fBpackage\-lock\.json\fP and
  140. \fBnpm\-shrinkwrap\.json\fP\|\. For example to run some postprocessing on the generated
  141. file:
  142. .P
  143. .RS 2
  144. .nf
  145. "scripts": {
  146. "postshrinkwrap": "json \-I \-e \\"this\.myMetadata = $MY_APP_METADATA\\""
  147. }
  148. .fi
  149. .RE
  150. .SS Using locked packages
  151. .P
  152. Using a locked package is no different than using any package without a package
  153. lock: any commands that update \fBnode_modules\fP and/or \fBpackage\.json\fP\|'s
  154. dependencies will automatically sync the existing lockfile\. This includes \fBnpm
  155. install\fP, \fBnpm rm\fP, \fBnpm update\fP, etc\. To prevent this update from happening,
  156. you can use the \fB\-\-no\-save\fP option to prevent saving altogether, or
  157. \fB\-\-no\-shrinkwrap\fP to allow \fBpackage\.json\fP to be updated while leaving
  158. \fBpackage\-lock\.json\fP or \fBnpm\-shrinkwrap\.json\fP intact\.
  159. .P
  160. It is highly recommended you commit the generated package lock to source
  161. control: this will allow anyone else on your team, your deployments, your
  162. CI/continuous integration, and anyone else who runs \fBnpm install\fP in your
  163. package source to get the exact same dependency tree that you were developing
  164. on\. Additionally, the diffs from these changes are human\-readable and will
  165. inform you of any changes npm has made to your \fBnode_modules\fP, so you can notice
  166. if any transitive dependencies were updated, hoisted, etc\.
  167. .SS Resolving lockfile conflicts
  168. .P
  169. Occasionally, two separate npm install will create package locks that cause
  170. merge conflicts in source control systems\. As of \fBnpm@5\.7\.0\fP, these conflicts
  171. can be resolved by manually fixing any \fBpackage\.json\fP conflicts, and then
  172. running \fBnpm install [\-\-package\-lock\-only]\fP again\. npm will automatically
  173. resolve any conflicts for you and write a merged package lock that includes all
  174. the dependencies from both branches in a reasonable tree\. If
  175. \fB\-\-package\-lock\-only\fP is provided, it will do this without also modifying your
  176. local \fBnode_modules/\fP\|\.
  177. .P
  178. To make this process seamless on git, consider installing
  179. \fBnpm\-merge\-driver\fP \fIhttps://npm\.im/npm\-merge\-driver\fR, which will teach git how
  180. to do this itself without any user interaction\. In short: \fB$ npx
  181. npm\-merge\-driver install \-g\fP will let you do this, and even works with
  182. pre\-\fBnpm@5\.7\.0\fP versions of npm 5, albeit a bit more noisily\. Note that if
  183. \fBpackage\.json\fP itself conflicts, you will have to resolve that by hand and run
  184. \fBnpm install\fP manually, even with the merge driver\.
  185. .SS See Also
  186. .RS 0
  187. .IP \(bu 2
  188. https://medium\.com/@sdboyer/so\-you\-want\-to\-write\-a\-package\-manager\-4ae9c17d9527
  189. .IP \(bu 2
  190. npm help package\.json
  191. .IP \(bu 2
  192. npm help package\-lock\.json
  193. .IP \(bu 2
  194. npm help shrinkwrap\.json
  195. .IP \(bu 2
  196. npm help shrinkwrap
  197. .RE