Team Ai
Datasetpublic

codekingpro/portable-devtools

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