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