Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
workspaces.html352 linesDownload Raw Back to using-npm
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
116header.title .version {
117    font-size: 0.8em;
118    color: #666666;
119}
120
121footer#edit {
122    border-top: solid 1px #e1e4e8;
123    margin: 3em 0 4em 0;
124    padding-top: 2em;
125}
126
127table {
128    width: 100%;
129    margin: 1em 0;
130    border-radius: 6px;
131    border: 1px solid #e1e4e8;
132    overflow: hidden;
133    border-collapse: separate;
134    border-spacing: 0;
135}
136
137table thead {
138    background-color: #f6f8fa;
139}
140
141table tbody {
142    background-color: #ffffff;
143}
144
145table th,
146table td {
147    padding: 0.75em;
148    text-align: left;
149    border-right: 1px solid #e1e4e8;
150    border-bottom: 1px solid #e1e4e8;
151}
152
153table th:last-child,
154table td:last-child {
155    border-right: none;
156}
157
158table tbody tr:last-child td {
159    border-bottom: none;
160}
161
162table th {
163    font-weight: 600;
164    background-color: #f6f8fa;
165}
166
167table code {
168    white-space: nowrap;
169}
170
171</style>
172</head>
173<body>
174<div id="banner">
175<div id="rainbar"></div>
176<div id="logobar">
177<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
178<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
179<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
180</svg>
181<div class="title">
182npm command-line interface
183</div>
184</div>
185</div>
186
187<section id="content">
188<header class="title">
189<h1 id="----workspaces----11121">
190    <span>Workspaces</span>
191    <span class="version">@11.12.1</span>
192</h1>
193<span class="description">Working with workspaces</span>
194</header>
195
196<section id="table_of_contents">
197<h2 id="table-of-contents">Table of contents</h2>
198<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>
199</section>
200
201<div id="_content"><h3 id="description">Description</h3>
202<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>
203<p>This set of features makes up for a much more streamlined workflow handling linked packages from the local file system.
204It 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>
205<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>
206<code>workspaces</code> configuration.</p>
207<h3 id="defining-workspaces">Defining workspaces</h3>
208<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>
209<pre><code class="language-json">{
210  "name": "my-workspaces-powered-project",
211  "workspaces": [
212    "packages/a"
213  ]
214}
215</code></pre>
216<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>
217<pre><code>.
218+-- package.json
219`-- packages
220   +-- a
221   |   `-- package.json
222</code></pre>
223<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>
224<p>Below is a post <code>npm install</code> example, given that same previous example structure of files and folders:</p>
225<pre><code>.
226+-- node_modules
227|  `-- a -&gt; ../packages/a
228+-- package-lock.json
229+-- package.json
230`-- packages
231   +-- a
232   |   `-- package.json
233</code></pre>
234<h3 id="getting-started-with-workspaces">Getting started with workspaces</h3>
235<p>You may automate the required steps to define a new workspace using <a href="../commands/npm-init.html">npm init</a>.
236For example in a project that already has a <code>package.json</code> defined you can run:</p>
237<pre><code>npm init -w ./packages/a
238</code></pre>
239<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
240<code>"workspaces"</code> property of your root project <code>package.json</code>.</p>
241<h3 id="adding-dependencies-to-a-workspace">Adding dependencies to a workspace</h3>
242<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>
243<p>For example, assuming the following structure:</p>
244<pre><code>.
245+-- package.json
246`-- packages
247   +-- a
248   |   `-- package.json
249   `-- b
250       `-- package.json
251</code></pre>
252<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>
253<pre><code>npm install abbrev -w a
254</code></pre>
255<p><strong>Adding a workspace as a dependency of another workspace:</strong></p>
256<p>The same approach works when adding one workspace as a dependency of another.
257If you want to add workspace <strong>b</strong> as a dependency of workspace <strong>a</strong>, run:</p>
258<pre><code>npm install b -w a
259</code></pre>
260<p>npm will detect that <strong>b</strong> is a workspace and automatically symlink it rather
261than fetching it from the registry. The resulting entry in workspace <strong>a</strong>'s
262<code>package.json</code> will use a standard version range:</p>
263<pre><code class="language-json">{
264  "dependencies": {
265    "b": "^1.0.0"
266  }
267}
268</code></pre>
269<p>Note: other installing commands such as <code>uninstall</code>, <code>ci</code>, etc will also respect the provided <code>workspace</code> configuration.</p>
270<h3 id="using-workspaces">Using workspaces</h3>
271<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>.
272Continuing 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>
273<pre><code>// ./packages/a/index.js
274module.exports = 'a'
275
276// ./lib/index.js
277const moduleA = require('a')
278console.log(moduleA) // -&gt; a
279</code></pre>
280<p>When running it with:</p>
281<p><code>node lib/index.js</code></p>
282<p>This demonstrates how the nature of <code>node_modules</code> resolution allows for
283<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>
284<h3 id="running-commands-in-the-context-of-workspaces">Running commands in the context of workspaces</h3>
285<p>You can use the <code>workspace</code> configuration option to run commands in the context of a configured workspace.
286Additionally, 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>
287<p>Following is a quick example on how to use the <code>npm run</code> command in the context of nested workspaces.
288For a project containing multiple workspaces, e.g:</p>
289<pre><code>.
290+-- package.json
291`-- packages
292   +-- a
293   |   `-- package.json
294   `-- b
295       `-- package.json
296</code></pre>
297<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.
298e.g:</p>
299<pre><code>npm run test --workspace=a
300</code></pre>
301<p>You could also run the command within the workspace.</p>
302<pre><code>cd packages/a &amp;&amp; npm run test
303</code></pre>
304<p>Either will run the <code>test</code> script defined within the
305<code>./packages/a/package.json</code> file.</p>
306<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>
307<pre><code>npm run test --workspace=a --workspace=b
308</code></pre>
309<p>Or run the command for each workspace within the 'packages' folder:</p>
310<pre><code>npm run test --workspace=packages
311</code></pre>
312<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.
313e.g:</p>
314<pre><code>npm run test --workspaces
315</code></pre>
316<p>Will run the <code>test</code> script in both <code>./packages/a</code> and <code>./packages/b</code>.</p>
317<p>Commands will be run in each workspace in the order they appear in your <code>package.json</code></p>
318<pre><code>{
319  "workspaces": [ "packages/a", "packages/b" ]
320}
321</code></pre>
322<p>Order of run is different with:</p>
323<pre><code>{
324  "workspaces": [ "packages/b", "packages/a" ]
325}
326</code></pre>
327<h3 id="ignoring-missing-scripts">Ignoring missing scripts</h3>
328<p>It is not required for all of the workspaces to implement scripts run with the <code>npm run</code> command.</p>
329<p>By running the command with the <code>--if-present</code> flag, npm will ignore workspaces missing target script.</p>
330<pre><code>npm run test --workspaces --if-present
331</code></pre>
332<h3 id="see-also">See also</h3>
333<ul>
334<li><a href="../commands/npm-install.html">npm install</a></li>
335<li><a href="../commands/npm-publish.html">npm publish</a></li>
336<li><a href="../commands/npm-run.html">npm run</a></li>
337<li><a href="../using-npm/config.html">config</a></li>
338</ul></div>
339
340<footer id="edit">
341<a href="https://github.com/npm/cli/edit/latest/docs/lib/content/using-npm/workspaces.md">
342<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
343<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>
344</svg>
345Edit this page on GitHub
346</a>
347</footer>
348</section>
349
350
351
352</body></html>
codekingpro/portable-devtools · Team Ai