workspaces.7 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. .TH "WORKSPACES" "7" "October 2021" "" ""
  2. .SH "NAME"
  3. \fBworkspaces\fR \- Working with workspaces
  4. .SS Description
  5. .P
  6. \fBWorkspaces\fR is a generic term that refers to the set of features in the
  7. npm cli that provides support to managing multiple packages from your local
  8. files system from within a singular top\-level, root package\.
  9. .P
  10. This set of features makes up for a much more streamlined workflow handling
  11. linked packages from the local file system\. Automating the linking process
  12. as part of \fBnpm install\fP and avoiding manually having to use \fBnpm link\fP in
  13. order to add references to packages that should be symlinked into the current
  14. \fBnode_modules\fP folder\.
  15. .P
  16. We also refer to these packages being auto\-symlinked during \fBnpm install\fP as a
  17. single \fBworkspace\fR, meaning it's a nested package within the current local
  18. file system that is explicitly defined in the npm help \fBpackage\.json\fP
  19. \fBworkspaces\fP configuration\.
  20. .SS Defining workspaces
  21. .P
  22. Workspaces are usually defined via the \fBworkspaces\fP property of the
  23. npm help \fBpackage\.json\fP file, e\.g:
  24. .P
  25. .RS 2
  26. .nf
  27. {
  28. "name": "my\-workspaces\-powered\-project",
  29. "workspaces": [
  30. "workspace\-a"
  31. ]
  32. }
  33. .fi
  34. .RE
  35. .P
  36. Given the above \fBpackage\.json\fP example living at a current working
  37. directory \fB\|\.\fP that contains a folder named \fBworkspace\-a\fP that itself contains
  38. a \fBpackage\.json\fP inside it, defining a Node\.js package, e\.g:
  39. .P
  40. .RS 2
  41. .nf
  42. \|\.
  43. +\-\- package\.json
  44. `\-\- workspace\-a
  45. `\-\- package\.json
  46. .fi
  47. .RE
  48. .P
  49. The expected result once running \fBnpm install\fP in this current working
  50. directory \fB\|\.\fP is that the folder \fBworkspace\-a\fP will get symlinked to the
  51. \fBnode_modules\fP folder of the current working dir\.
  52. .P
  53. Below is a post \fBnpm install\fP example, given that same previous example
  54. structure of files and folders:
  55. .P
  56. .RS 2
  57. .nf
  58. \|\.
  59. +\-\- node_modules
  60. | `\-\- workspace\-a \-> \.\./workspace\-a
  61. +\-\- package\-lock\.json
  62. +\-\- package\.json
  63. `\-\- workspace\-a
  64. `\-\- package\.json
  65. .fi
  66. .RE
  67. .SS Getting started with workspaces
  68. .P
  69. You may automate the required steps to define a new workspace using
  70. npm help init\. For example in a project that already has a
  71. \fBpackage\.json\fP defined you can run:
  72. .P
  73. .RS 2
  74. .nf
  75. npm init \-w \./packages/a
  76. .fi
  77. .RE
  78. .P
  79. This command will create the missing folders and a new \fBpackage\.json\fP
  80. file (if needed) while also making sure to properly configure the
  81. \fB"workspaces"\fP property of your root project \fBpackage\.json\fP\|\.
  82. .SS Adding dependencies to a workspace
  83. .P
  84. It's possible to directly add/remove/update dependencies of your workspaces
  85. using the npm help \fBworkspace\fP config\.
  86. .P
  87. For example, assuming the following structure:
  88. .P
  89. .RS 2
  90. .nf
  91. \|\.
  92. +\-\- package\.json
  93. `\-\- packages
  94. +\-\- a
  95. | `\-\- package\.json
  96. `\-\- b
  97. `\-\- package\.json
  98. .fi
  99. .RE
  100. .P
  101. If you want to add a dependency named \fBabbrev\fP from the registry as a
  102. dependency of your workspace \fBa\fR, you may use the workspace config to tell
  103. the npm installer that package should be added as a dependency of the provided
  104. workspace:
  105. .P
  106. .RS 2
  107. .nf
  108. npm install abbrev \-w a
  109. .fi
  110. .RE
  111. .P
  112. Note: other installing commands such as \fBuninstall\fP, \fBci\fP, etc will also
  113. respect the provided \fBworkspace\fP configuration\.
  114. .SS Using workspaces
  115. .P
  116. Given the specifities of how Node\.js handles module resolution \fIhttps://nodejs\.org/dist/latest\-v14\.x/docs/api/modules\.html#modules_all_together\fR it's possible to consume any defined workspace
  117. by its declared \fBpackage\.json\fP \fBname\fP\|\. Continuing from the example defined
  118. above, let's also create a Node\.js script that will require the \fBworkspace\-a\fP
  119. example module, e\.g:
  120. .P
  121. .RS 2
  122. .nf
  123. // \./workspace\-a/index\.js
  124. module\.exports = 'a'
  125. // \./lib/index\.js
  126. const moduleA = require('workspace\-a')
  127. console\.log(moduleA) // \-> a
  128. .fi
  129. .RE
  130. .P
  131. When running it with:
  132. .P
  133. \fBnode lib/index\.js\fP
  134. .P
  135. This demonstrates how the nature of \fBnode_modules\fP resolution allows for
  136. \fBworkspaces\fR to enable a portable workflow for requiring each \fBworkspace\fR
  137. in such a way that is also easy to npm help publish these
  138. nested workspaces to be consumed elsewhere\.
  139. .SS Running commands in the context of workspaces
  140. .P
  141. You can use the \fBworkspace\fP configuration option to run commands in the context
  142. of a configured workspace\.
  143. .P
  144. Following is a quick example on how to use the \fBnpm run\fP command in the context
  145. of nested workspaces\. For a project containing multiple workspaces, e\.g:
  146. .P
  147. .RS 2
  148. .nf
  149. \|\.
  150. +\-\- package\.json
  151. `\-\- packages
  152. +\-\- a
  153. | `\-\- package\.json
  154. `\-\- b
  155. `\-\- package\.json
  156. .fi
  157. .RE
  158. .P
  159. By running a command using the \fBworkspace\fP option, it's possible to run the
  160. given command in the context of that specific workspace\. e\.g:
  161. .P
  162. .RS 2
  163. .nf
  164. npm run test \-\-workspace=a
  165. .fi
  166. .RE
  167. .P
  168. This will run the \fBtest\fP script defined within the
  169. \fB\|\./packages/a/package\.json\fP file\.
  170. .P
  171. Please note that you can also specify this argument multiple times in the
  172. command\-line in order to target multiple workspaces, e\.g:
  173. .P
  174. .RS 2
  175. .nf
  176. npm run test \-\-workspace=a \-\-workspace=b
  177. .fi
  178. .RE
  179. .P
  180. It's also possible to use the \fBworkspaces\fP (plural) configuration option to
  181. enable the same behavior but running that command in the context of \fBall\fR
  182. configured workspaces\. e\.g:
  183. .P
  184. .RS 2
  185. .nf
  186. npm run test \-\-workspaces
  187. .fi
  188. .RE
  189. .P
  190. Will run the \fBtest\fP script in both \fB\|\./packages/a\fP and \fB\|\./packages/b\fP\|\.
  191. .P
  192. Commands will be run in each workspace in the order they appear in your \fBpackage\.json\fP
  193. .P
  194. .RS 2
  195. .nf
  196. {
  197. "workspaces": [ "packages/a", "packages/b" ]
  198. }
  199. .fi
  200. .RE
  201. .P
  202. Order of run is different with:
  203. .P
  204. .RS 2
  205. .nf
  206. {
  207. "workspaces": [ "packages/b", "packages/a" ]
  208. }
  209. .fi
  210. .RE
  211. .SS Ignoring missing scripts
  212. .P
  213. It is not required for all of the workspaces to implement scripts run with the \fBnpm run\fP command\.
  214. .P
  215. By running the command with the \fB\-\-if\-present\fP flag, npm will ignore workspaces missing target script\.
  216. .P
  217. .RS 2
  218. .nf
  219. npm run test \-\-workspaces \-\-if\-present
  220. .fi
  221. .RE
  222. .SS See also
  223. .RS 0
  224. .IP \(bu 2
  225. npm help install
  226. .IP \(bu 2
  227. npm help publish
  228. .IP \(bu 2
  229. npm help run\-script
  230. .IP \(bu 2
  231. npm help config
  232. .RE