workspaces.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <html><head>
  2. <title>workspaces</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="workspaces">workspaces</h1>
  125. <span class="description">Working with workspaces</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><li><a href="#defining-workspaces">Defining workspaces</a></li><li><a href="#getting-started-with-workspaces">Getting started with workspaces</a></li><li><a href="#adding-dependencies-to-a-workspace">Adding dependencies to a workspace</a></li><li><a href="#using-workspaces">Using workspaces</a></li><li><a href="#running-commands-in-the-context-of-workspaces">Running commands in the context of workspaces</a></li><li><a href="#ignoring-missing-scripts">Ignoring missing scripts</a></li><li><a href="#see-also">See also</a></li></ul></div>
  130. </section>
  131. <div id="_content"><h3 id="description">Description</h3>
  132. <p><strong>Workspaces</strong> is a generic term that refers to the set of features in the
  133. npm cli that provides support to managing multiple packages from your local
  134. files system from within a singular top-level, root package.</p>
  135. <p>This set of features makes up for a much more streamlined workflow handling
  136. linked packages from the local file system. Automating the linking process
  137. as part of <code>npm install</code> and avoiding manually having to use <code>npm link</code> in
  138. order to add references to packages that should be symlinked into the current
  139. <code>node_modules</code> folder.</p>
  140. <p>We also refer to these packages being auto-symlinked during <code>npm install</code> as a
  141. single <strong>workspace</strong>, meaning it’s a nested package within the current local
  142. file system that is explicitly defined in the <a href="../configuring-npm/package-json#workspaces.html"><code>package.json</code></a>
  143. <code>workspaces</code> configuration.</p>
  144. <h3 id="defining-workspaces">Defining workspaces</h3>
  145. <p>Workspaces are usually defined via the <code>workspaces</code> property of the
  146. <a href="../configuring-npm/package-json#workspaces.html"><code>package.json</code></a> file, e.g:</p>
  147. <pre lang="json"><code>{
  148. "name": "my-workspaces-powered-project",
  149. "workspaces": [
  150. "workspace-a"
  151. ]
  152. }
  153. </code></pre>
  154. <p>Given the above <code>package.json</code> example living at a current working
  155. directory <code>.</code> that contains a folder named <code>workspace-a</code> that itself contains
  156. a <code>package.json</code> inside it, defining a Node.js package, e.g:</p>
  157. <pre><code>.
  158. +-- package.json
  159. `-- workspace-a
  160. `-- package.json
  161. </code></pre>
  162. <p>The expected result once running <code>npm install</code> in this current working
  163. directory <code>.</code> is that the folder <code>workspace-a</code> will get symlinked to the
  164. <code>node_modules</code> folder of the current working dir.</p>
  165. <p>Below is a post <code>npm install</code> example, given that same previous example
  166. structure of files and folders:</p>
  167. <pre><code>.
  168. +-- node_modules
  169. | `-- workspace-a -&gt; ../workspace-a
  170. +-- package-lock.json
  171. +-- package.json
  172. `-- workspace-a
  173. `-- package.json
  174. </code></pre>
  175. <h3 id="getting-started-with-workspaces">Getting started with workspaces</h3>
  176. <p>You may automate the required steps to define a new workspace using
  177. <a href="../commands/npm-init.html">npm init</a>. For example in a project that already has a
  178. <code>package.json</code> defined you can run:</p>
  179. <pre><code>npm init -w ./packages/a
  180. </code></pre>
  181. <p>This command will create the missing folders and a new <code>package.json</code>
  182. file (if needed) while also making sure to properly configure the
  183. <code>"workspaces"</code> property of your root project <code>package.json</code>.</p>
  184. <h3 id="adding-dependencies-to-a-workspace">Adding dependencies to a workspace</h3>
  185. <p>It’s possible to directly add/remove/update dependencies of your workspaces
  186. using the <a href="../using-npm/config#workspace.html"><code>workspace</code> config</a>.</p>
  187. <p>For example, assuming the following structure:</p>
  188. <pre><code>.
  189. +-- package.json
  190. `-- packages
  191. +-- a
  192. | `-- package.json
  193. `-- b
  194. `-- package.json
  195. </code></pre>
  196. <p>If you want to add a dependency named <code>abbrev</code> from the registry as a
  197. dependency of your workspace <strong>a</strong>, you may use the workspace config to tell
  198. the npm installer that package should be added as a dependency of the provided
  199. workspace:</p>
  200. <pre><code>npm install abbrev -w a
  201. </code></pre>
  202. <p>Note: other installing commands such as <code>uninstall</code>, <code>ci</code>, etc will also
  203. respect the provided <code>workspace</code> configuration.</p>
  204. <h3 id="using-workspaces">Using workspaces</h3>
  205. <p>Given the <a href="https://nodejs.org/dist/latest-v14.x/docs/api/modules.html#modules_all_together">specifities of how Node.js handles module resolution</a> it’s possible to consume any defined workspace
  206. by its declared <code>package.json</code> <code>name</code>. Continuing from the example defined
  207. above, let’s also create a Node.js script that will require the <code>workspace-a</code>
  208. example module, e.g:</p>
  209. <pre><code>// ./workspace-a/index.js
  210. module.exports = 'a'
  211. // ./lib/index.js
  212. const moduleA = require('workspace-a')
  213. console.log(moduleA) // -&gt; a
  214. </code></pre>
  215. <p>When running it with:</p>
  216. <p><code>node lib/index.js</code></p>
  217. <p>This demonstrates how the nature of <code>node_modules</code> resolution allows for
  218. <strong>workspaces</strong> to enable a portable workflow for requiring each <strong>workspace</strong>
  219. in such a way that is also easy to <a href="../commands/npm-publish.html">publish</a> these
  220. nested workspaces to be consumed elsewhere.</p>
  221. <h3 id="running-commands-in-the-context-of-workspaces">Running commands in the context of workspaces</h3>
  222. <p>You can use the <code>workspace</code> configuration option to run commands in the context
  223. of a configured workspace.</p>
  224. <p>Following is a quick example on how to use the <code>npm run</code> command in the context
  225. of nested workspaces. For a project containing multiple workspaces, e.g:</p>
  226. <pre><code>.
  227. +-- package.json
  228. `-- packages
  229. +-- a
  230. | `-- package.json
  231. `-- b
  232. `-- package.json
  233. </code></pre>
  234. <p>By running a command using the <code>workspace</code> option, it’s possible to run the
  235. given command in the context of that specific workspace. e.g:</p>
  236. <pre><code>npm run test --workspace=a
  237. </code></pre>
  238. <p>This will run the <code>test</code> script defined within the
  239. <code>./packages/a/package.json</code> file.</p>
  240. <p>Please note that you can also specify this argument multiple times in the
  241. command-line in order to target multiple workspaces, e.g:</p>
  242. <pre><code>npm run test --workspace=a --workspace=b
  243. </code></pre>
  244. <p>It’s also possible to use the <code>workspaces</code> (plural) configuration option to
  245. enable the same behavior but running that command in the context of <strong>all</strong>
  246. configured workspaces. e.g:</p>
  247. <pre><code>npm run test --workspaces
  248. </code></pre>
  249. <p>Will run the <code>test</code> script in both <code>./packages/a</code> and <code>./packages/b</code>.</p>
  250. <p>Commands will be run in each workspace in the order they appear in your <code>package.json</code></p>
  251. <pre><code>{
  252. "workspaces": [ "packages/a", "packages/b" ]
  253. }
  254. </code></pre>
  255. <p>Order of run is different with:</p>
  256. <pre><code>{
  257. "workspaces": [ "packages/b", "packages/a" ]
  258. }
  259. </code></pre>
  260. <h3 id="ignoring-missing-scripts">Ignoring missing scripts</h3>
  261. <p>It is not required for all of the workspaces to implement scripts run with the <code>npm run</code> command.</p>
  262. <p>By running the command with the <code>--if-present</code> flag, npm will ignore workspaces missing target script.</p>
  263. <pre><code>npm run test --workspaces --if-present
  264. </code></pre>
  265. <h3 id="see-also">See also</h3>
  266. <ul>
  267. <li><a href="../commands/npm-install.html">npm install</a></li>
  268. <li><a href="../commands/npm-publish.html">npm publish</a></li>
  269. <li><a href="../commands/npm-run-script.html">npm run-script</a></li>
  270. <li><a href="../using-npm/config.html">config</a></li>
  271. </ul>
  272. </div>
  273. <footer id="edit">
  274. <a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/workspaces.md">
  275. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  276. <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>
  277. </svg>
  278. Edit this page on GitHub
  279. </a>
  280. </footer>
  281. </section>
  282. </body></html>