workspaces.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <!DOCTYPE html><html><head>
  2. <meta charset="utf-8">
  3. <title>Workspaces</title>
  4. <style>
  5. body {
  6. background-color: #ffffff;
  7. color: #24292e;
  8. margin: 0;
  9. line-height: 1.5;
  10. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  11. }
  12. #rainbar {
  13. height: 10px;
  14. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  15. }
  16. a {
  17. text-decoration: none;
  18. color: #0366d6;
  19. }
  20. a:hover {
  21. text-decoration: underline;
  22. }
  23. pre {
  24. margin: 1em 0px;
  25. padding: 1em;
  26. border: solid 1px #e1e4e8;
  27. border-radius: 6px;
  28. display: block;
  29. overflow: auto;
  30. white-space: pre;
  31. background-color: #f6f8fa;
  32. color: #393a34;
  33. }
  34. code {
  35. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  36. font-size: 85%;
  37. padding: 0.2em 0.4em;
  38. background-color: #f6f8fa;
  39. color: #393a34;
  40. }
  41. pre > code {
  42. padding: 0;
  43. background-color: inherit;
  44. color: inherit;
  45. }
  46. h1, h2, h3 {
  47. font-weight: 600;
  48. }
  49. #logobar {
  50. background-color: #333333;
  51. margin: 0 auto;
  52. padding: 1em 4em;
  53. }
  54. #logobar .logo {
  55. float: left;
  56. }
  57. #logobar .title {
  58. font-weight: 600;
  59. color: #dddddd;
  60. float: left;
  61. margin: 5px 0 0 1em;
  62. }
  63. #logobar:after {
  64. content: "";
  65. display: block;
  66. clear: both;
  67. }
  68. #content {
  69. margin: 0 auto;
  70. padding: 0 4em;
  71. }
  72. #table_of_contents > h2 {
  73. font-size: 1.17em;
  74. }
  75. #table_of_contents ul:first-child {
  76. border: solid 1px #e1e4e8;
  77. border-radius: 6px;
  78. padding: 1em;
  79. background-color: #f6f8fa;
  80. color: #393a34;
  81. }
  82. #table_of_contents ul {
  83. list-style-type: none;
  84. padding-left: 1.5em;
  85. }
  86. #table_of_contents li {
  87. font-size: 0.9em;
  88. }
  89. #table_of_contents li a {
  90. color: #000000;
  91. }
  92. header.title {
  93. border-bottom: solid 1px #e1e4e8;
  94. }
  95. header.title > h1 {
  96. margin-bottom: 0.25em;
  97. }
  98. header.title > .description {
  99. display: block;
  100. margin-bottom: 0.5em;
  101. line-height: 1;
  102. }
  103. header.title .version {
  104. font-size: 0.8em;
  105. color: #666666;
  106. }
  107. footer#edit {
  108. border-top: solid 1px #e1e4e8;
  109. margin: 3em 0 4em 0;
  110. padding-top: 2em;
  111. }
  112. table {
  113. width: 100%;
  114. margin: 1em 0;
  115. border-radius: 6px;
  116. border: 1px solid #e1e4e8;
  117. overflow: hidden;
  118. border-collapse: separate;
  119. border-spacing: 0;
  120. }
  121. table thead {
  122. background-color: #f6f8fa;
  123. }
  124. table tbody {
  125. background-color: #ffffff;
  126. }
  127. table th,
  128. table td {
  129. padding: 0.75em;
  130. text-align: left;
  131. border-right: 1px solid #e1e4e8;
  132. border-bottom: 1px solid #e1e4e8;
  133. }
  134. table th:last-child,
  135. table td:last-child {
  136. border-right: none;
  137. }
  138. table tbody tr:last-child td {
  139. border-bottom: none;
  140. }
  141. table th {
  142. font-weight: 600;
  143. background-color: #f6f8fa;
  144. }
  145. table code {
  146. white-space: nowrap;
  147. }
  148. </style>
  149. </head>
  150. <body>
  151. <div id="banner">
  152. <div id="rainbar"></div>
  153. <div id="logobar">
  154. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  155. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  156. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  157. </svg>
  158. <div class="title">
  159. npm command-line interface
  160. </div>
  161. </div>
  162. </div>
  163. <section id="content">
  164. <header class="title">
  165. <h1 id="----workspaces----11120">
  166. <span>Workspaces</span>
  167. <span class="version">@11.12.0</span>
  168. </h1>
  169. <span class="description">Working with workspaces</span>
  170. </header>
  171. <section id="table_of_contents">
  172. <h2 id="table-of-contents">Table of contents</h2>
  173. <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>
  174. </section>
  175. <div id="_content"><h3 id="description">Description</h3>
  176. <p><strong>Workspaces</strong> is a generic term that refers to the set of features in the npm cli that provides support for managing multiple packages from your local file system from within a singular top-level, root package.</p>
  177. <p>This set of features makes up for a much more streamlined workflow handling linked packages from the local file system.
  178. It automates the linking process as part of <code>npm install</code> and removes the need to manually use <code>npm link</code> in order to add references to packages that should be symlinked into the current <code>node_modules</code> folder.</p>
  179. <p>We also refer to these packages being auto-symlinked during <code>npm install</code> as a single <strong>workspace</strong>, meaning it's a nested package within the current local file system that is explicitly defined in the <a href="../configuring-npm/package-json#workspaces.html"><code>package.json</code></a>
  180. <code>workspaces</code> configuration.</p>
  181. <h3 id="defining-workspaces">Defining workspaces</h3>
  182. <p>Workspaces are usually defined via the <code>workspaces</code> property of the <a href="../configuring-npm/package-json#workspaces.html"><code>package.json</code></a> file, e.g:</p>
  183. <pre><code class="language-json">{
  184. "name": "my-workspaces-powered-project",
  185. "workspaces": [
  186. "packages/a"
  187. ]
  188. }
  189. </code></pre>
  190. <p>Given the above <code>package.json</code> example living at a current working directory <code>.</code> that contains a folder named <code>packages/a</code> that itself contains a <code>package.json</code> inside it, defining a Node.js package, e.g:</p>
  191. <pre><code>.
  192. +-- package.json
  193. `-- packages
  194. +-- a
  195. | `-- package.json
  196. </code></pre>
  197. <p>The expected result once running <code>npm install</code> in this current working directory <code>.</code> is that the folder <code>packages/a</code> will get symlinked to the <code>node_modules</code> folder of the current working dir.</p>
  198. <p>Below is a post <code>npm install</code> example, given that same previous example structure of files and folders:</p>
  199. <pre><code>.
  200. +-- node_modules
  201. | `-- a -&gt; ../packages/a
  202. +-- package-lock.json
  203. +-- package.json
  204. `-- packages
  205. +-- a
  206. | `-- package.json
  207. </code></pre>
  208. <h3 id="getting-started-with-workspaces">Getting started with workspaces</h3>
  209. <p>You may automate the required steps to define a new workspace using <a href="../commands/npm-init.html">npm init</a>.
  210. For example in a project that already has a <code>package.json</code> defined you can run:</p>
  211. <pre><code>npm init -w ./packages/a
  212. </code></pre>
  213. <p>This command will create the missing folders and a new <code>package.json</code> file (if needed) while also making sure to properly configure the
  214. <code>"workspaces"</code> property of your root project <code>package.json</code>.</p>
  215. <h3 id="adding-dependencies-to-a-workspace">Adding dependencies to a workspace</h3>
  216. <p>It's possible to directly add/remove/update dependencies of your workspaces using the <a href="../using-npm/config#workspace.html"><code>workspace</code> config</a>.</p>
  217. <p>For example, assuming the following structure:</p>
  218. <pre><code>.
  219. +-- package.json
  220. `-- packages
  221. +-- a
  222. | `-- package.json
  223. `-- b
  224. `-- package.json
  225. </code></pre>
  226. <p>If you want to add a dependency named <code>abbrev</code> from the registry as a dependency of your workspace <strong>a</strong>, you may use the workspace config to tell the npm installer that package should be added as a dependency of the provided workspace:</p>
  227. <pre><code>npm install abbrev -w a
  228. </code></pre>
  229. <p><strong>Adding a workspace as a dependency of another workspace:</strong></p>
  230. <p>The same approach works when adding one workspace as a dependency of another.
  231. If you want to add workspace <strong>b</strong> as a dependency of workspace <strong>a</strong>, run:</p>
  232. <pre><code>npm install b -w a
  233. </code></pre>
  234. <p>npm will detect that <strong>b</strong> is a workspace and automatically symlink it rather
  235. than fetching it from the registry. The resulting entry in workspace <strong>a</strong>'s
  236. <code>package.json</code> will use a standard version range:</p>
  237. <pre><code class="language-json">{
  238. "dependencies": {
  239. "b": "^1.0.0"
  240. }
  241. }
  242. </code></pre>
  243. <p>Note: other installing commands such as <code>uninstall</code>, <code>ci</code>, etc will also respect the provided <code>workspace</code> configuration.</p>
  244. <h3 id="using-workspaces">Using workspaces</h3>
  245. <p>Given the <a href="https://nodejs.org/dist/latest-v14.x/docs/api/modules.html#modules_all_together">specifics of how Node.js handles module resolution</a> it's possible to consume any defined workspace by its declared <code>package.json</code> <code>name</code>.
  246. Continuing from the example defined above, let's also create a Node.js script that will require the workspace <code>a</code> example module, e.g:</p>
  247. <pre><code>// ./packages/a/index.js
  248. module.exports = 'a'
  249. // ./lib/index.js
  250. const moduleA = require('a')
  251. console.log(moduleA) // -&gt; a
  252. </code></pre>
  253. <p>When running it with:</p>
  254. <p><code>node lib/index.js</code></p>
  255. <p>This demonstrates how the nature of <code>node_modules</code> resolution allows for
  256. <strong>workspaces</strong> to enable a portable workflow for requiring each <strong>workspace</strong> in such a way that is also easy to <a href="../commands/npm-publish.html">publish</a> these nested workspaces to be consumed elsewhere.</p>
  257. <h3 id="running-commands-in-the-context-of-workspaces">Running commands in the context of workspaces</h3>
  258. <p>You can use the <code>workspace</code> configuration option to run commands in the context of a configured workspace.
  259. Additionally, if your current directory is in a workspace, the <code>workspace</code> configuration is implicitly set, and <code>prefix</code> is set to the root workspace.</p>
  260. <p>Following is a quick example on how to use the <code>npm run</code> command in the context of nested workspaces.
  261. For a project containing multiple workspaces, e.g:</p>
  262. <pre><code>.
  263. +-- package.json
  264. `-- packages
  265. +-- a
  266. | `-- package.json
  267. `-- b
  268. `-- package.json
  269. </code></pre>
  270. <p>By running a command using the <code>workspace</code> option, it's possible to run the given command in the context of that specific workspace.
  271. e.g:</p>
  272. <pre><code>npm run test --workspace=a
  273. </code></pre>
  274. <p>You could also run the command within the workspace.</p>
  275. <pre><code>cd packages/a &amp;&amp; npm run test
  276. </code></pre>
  277. <p>Either will run the <code>test</code> script defined within the
  278. <code>./packages/a/package.json</code> file.</p>
  279. <p>Please note that you can also specify this argument multiple times in the command-line in order to target multiple workspaces, e.g:</p>
  280. <pre><code>npm run test --workspace=a --workspace=b
  281. </code></pre>
  282. <p>Or run the command for each workspace within the 'packages' folder:</p>
  283. <pre><code>npm run test --workspace=packages
  284. </code></pre>
  285. <p>It's also possible to use the <code>workspaces</code> (plural) configuration option to enable the same behavior but running that command in the context of <strong>all</strong> configured workspaces.
  286. e.g:</p>
  287. <pre><code>npm run test --workspaces
  288. </code></pre>
  289. <p>Will run the <code>test</code> script in both <code>./packages/a</code> and <code>./packages/b</code>.</p>
  290. <p>Commands will be run in each workspace in the order they appear in your <code>package.json</code></p>
  291. <pre><code>{
  292. "workspaces": [ "packages/a", "packages/b" ]
  293. }
  294. </code></pre>
  295. <p>Order of run is different with:</p>
  296. <pre><code>{
  297. "workspaces": [ "packages/b", "packages/a" ]
  298. }
  299. </code></pre>
  300. <h3 id="ignoring-missing-scripts">Ignoring missing scripts</h3>
  301. <p>It is not required for all of the workspaces to implement scripts run with the <code>npm run</code> command.</p>
  302. <p>By running the command with the <code>--if-present</code> flag, npm will ignore workspaces missing target script.</p>
  303. <pre><code>npm run test --workspaces --if-present
  304. </code></pre>
  305. <h3 id="see-also">See also</h3>
  306. <ul>
  307. <li><a href="../commands/npm-install.html">npm install</a></li>
  308. <li><a href="../commands/npm-publish.html">npm publish</a></li>
  309. <li><a href="../commands/npm-run.html">npm run</a></li>
  310. <li><a href="../using-npm/config.html">config</a></li>
  311. </ul></div>
  312. <footer id="edit">
  313. <a href="https://github.com/npm/cli/edit/latest/docs/lib/content/using-npm/workspaces.md">
  314. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  315. <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>
  316. </svg>
  317. Edit this page on GitHub
  318. </a>
  319. </footer>
  320. </section>
  321. </body></html>