2022-11-07 13:02:05 -05:00
.TH "WORKSPACES" "7" "November 2022" "" ""
2020-10-15 20:32:02 -04:00
.SH "NAME"
2020-11-01 07:54:36 +01:00
\fB workspaces\fR \- Working with workspaces
2020-10-15 20:32:02 -04:00
.SS Description
.P
\fB Workspaces\fR is a generic term that refers to the set of features in the
npm cli that provides support to managing multiple packages from your local
2021-12-02 22:04:46 +00:00
file system from within a singular top\- level, root package\.
2020-10-15 20:32:02 -04:00
.P
This set of features makes up for a much more streamlined workflow handling
linked packages from the local file system\. Automating the linking process
as part of \fB npm install\fP and avoiding manually having to use \fB npm link\fP in
order to add references to packages that should be symlinked into the current
\fB node_modules\fP folder\.
.P
We also refer to these packages being auto\- symlinked during \fB npm install\fP as a
single \fB workspace\fR , meaning it's a nested package within the current local
2022-11-07 13:02:05 -05:00
file system that is explicitly defined in the \fB package\. json\fP \fI /configuring\- npm/package\- json#workspaces\fR
2020-10-15 20:32:02 -04:00
\fB workspaces\fP configuration\.
2021-06-10 20:49:57 +00:00
.SS Defining workspaces
2020-10-15 20:32:02 -04:00
.P
Workspaces are usually defined via the \fB workspaces\fP property of the
2022-11-07 13:02:05 -05:00
\fB package\. json\fP \fI /configuring\- npm/package\- json#workspaces\fR file, e\. g:
2020-10-15 20:32:02 -04:00
.P
.RS 2
.nf
{
"name": "my\- workspaces\- powered\- project",
"workspaces": [
2022-02-19 05:11:58 +02:00
"packages/a"
2020-10-15 20:32:02 -04:00
]
}
.fi
.RE
.P
Given the above \fB package\. json\fP example living at a current working
2022-02-19 05:11:58 +02:00
directory \fB \| \. \fP that contains a folder named \fB packages/a\fP that itself contains
2021-07-15 20:09:18 +00:00
a \fB package\. json\fP inside it, defining a Node\. js package, e\. g:
2020-10-15 20:32:02 -04:00
.P
.RS 2
.nf
\| \.
+\- \- package\. json
2022-02-19 05:11:58 +02:00
`\- \- packages
+\- \- a
| `\- \- package\. json
2020-10-15 20:32:02 -04:00
.fi
.RE
.P
The expected result once running \fB npm install\fP in this current working
2022-02-19 05:11:58 +02:00
directory \fB \| \. \fP is that the folder \fB packages/a\fP will get symlinked to the
2020-10-15 20:32:02 -04:00
\fB node_modules\fP folder of the current working dir\.
.P
Below is a post \fB npm install\fP example, given that same previous example
structure of files and folders:
.P
.RS 2
.nf
\| \.
+\- \- node_modules
2022-07-19 08:51:49 -07:00
| `\- \- a \- > \. \. /packages/a
2020-10-15 20:32:02 -04:00
+\- \- package\- lock\. json
+\- \- package\. json
2022-02-19 05:11:58 +02:00
`\- \- packages
+\- \- a
| `\- \- package\. json
2020-10-15 20:32:02 -04:00
.fi
.RE
2021-06-10 20:49:57 +00:00
.SS Getting started with workspaces
.P
You may automate the required steps to define a new workspace using
npm help init\. For example in a project that already has a
\fB package\. json\fP defined you can run:
.P
.RS 2
.nf
npm init \- w \. /packages/a
.fi
.RE
.P
This command will create the missing folders and a new \fB package\. json\fP
file (if needed) while also making sure to properly configure the
\fB "workspaces"\fP property of your root project \fB package\. json\fP \| \.
.SS Adding dependencies to a workspace
.P
It's possible to directly add/remove/update dependencies of your workspaces
2022-11-07 13:02:05 -05:00
using the \fB workspace\fP config \fI /using\- npm/config#workspace\fR \| \.
2021-06-10 20:49:57 +00:00
.P
For example, assuming the following structure:
.P
.RS 2
.nf
\| \.
+\- \- package\. json
`\- \- packages
+\- \- a
| `\- \- package\. json
`\- \- b
`\- \- package\. json
.fi
.RE
.P
If you want to add a dependency named \fB abbrev\fP from the registry as a
dependency of your workspace \fB a\fR , you may use the workspace config to tell
the npm installer that package should be added as a dependency of the provided
workspace:
.P
.RS 2
.nf
npm install abbrev \- w a
.fi
.RE
.P
Note: other installing commands such as \fB uninstall\fP , \fB ci\fP , etc will also
respect the provided \fB workspace\fP configuration\.
2020-10-15 20:32:02 -04:00
.SS Using workspaces
.P
Given the specifities of how Node\. js handles module resolution \fI https://nodejs\. org/dist/latest\- v14\. x/docs/api/modules\. html#modules_all_together\fR it's possible to consume any defined workspace
2021-10-14 22:17:47 +00:00
by its declared \fB package\. json\fP \fB name\fP \| \. Continuing from the example defined
2022-07-19 08:51:49 -07:00
above, let's also create a Node\. js script that will require the workspace \fB a\fP
2020-10-15 20:32:02 -04:00
example module, e\. g:
.P
.RS 2
.nf
2022-07-19 08:51:49 -07:00
// \. /packages/a/index\. js
2020-10-15 20:32:02 -04:00
module\. exports = 'a'
// \. /lib/index\. js
2022-07-19 08:51:49 -07:00
const moduleA = require('a')
2020-10-15 20:32:02 -04:00
console\. log(moduleA) // \- > a
.fi
.RE
.P
When running it with:
.P
\fB node lib/index\. js\fP
.P
This demonstrates how the nature of \fB node_modules\fP resolution allows for
\fB workspaces\fR to enable a portable workflow for requiring each \fB workspace\fR
in such a way that is also easy to npm help publish these
nested workspaces to be consumed elsewhere\.
2021-03-23 14:58:11 -04:00
.SS Running commands in the context of workspaces
.P
2021-03-29 15:27:51 -04:00
You can use the \fB workspace\fP configuration option to run commands in the context
2021-03-23 14:58:11 -04:00
of a configured workspace\.
2022-07-28 11:03:27 -07:00
Additionally, if your current directory is in a workspace, the \fB workspace\fP
configuration is implicitly set, and \fB prefix\fP is set to the root workspace\.
2021-03-23 14:58:11 -04:00
.P
Following is a quick example on how to use the \fB npm run\fP command in the context
of nested workspaces\. For a project containing multiple workspaces, e\. g:
.P
.RS 2
.nf
\| \.
+\- \- package\. json
`\- \- packages
+\- \- a
| `\- \- package\. json
`\- \- b
`\- \- package\. json
.fi
.RE
.P
By running a command using the \fB workspace\fP option, it's possible to run the
given command in the context of that specific workspace\. e\. g:
.P
.RS 2
.nf
npm run test \- \- workspace=a
.fi
.RE
.P
2022-07-28 11:03:27 -07:00
You could also run the command within the workspace\.
.P
.RS 2
.nf
cd packages/a && npm run test
.fi
.RE
.P
Either will run the \fB test\fP script defined within the
2021-03-23 14:58:11 -04:00
\fB \| \. /packages/a/package\. json\fP file\.
.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
.RS 2
.nf
npm run test \- \- workspace=a \- \- workspace=b
.fi
.RE
.P
It's also possible to use the \fB workspaces\fP (plural) configuration option to
enable the same behavior but running that command in the context of \fB all\fR
configured workspaces\. e\. g:
.P
.RS 2
.nf
npm run test \- \- workspaces
.fi
.RE
.P
Will run the \fB test\fP script in both \fB \| \. /packages/a\fP and \fB \| \. /packages/b\fP \| \.
2021-10-07 20:21:11 +00:00
.P
Commands will be run in each workspace in the order they appear in your \fB package\. json\fP
.P
.RS 2
.nf
{
"workspaces": [ "packages/a", "packages/b" ]
}
.fi
.RE
.P
Order of run is different with:
.P
.RS 2
.nf
{
"workspaces": [ "packages/b", "packages/a" ]
}
.fi
.RE
2021-09-09 20:01:11 +00:00
.SS Ignoring missing scripts
.P
It is not required for all of the workspaces to implement scripts run with the \fB npm run\fP command\.
.P
By running the command with the \fB \- \- if\- present\fP flag, npm will ignore workspaces missing target script\.
.P
.RS 2
.nf
npm run test \- \- workspaces \- \- if\- present
.fi
.RE
2020-10-15 20:32:02 -04:00
.SS See also
.RS 0
.IP \(bu 2
npm help install
.IP \(bu 2
npm help publish
2021-03-23 14:58:11 -04:00
.IP \(bu 2
npm help run\- script
2021-06-10 20:49:57 +00:00
.IP \(bu 2
npm help config
2020-10-15 20:32:02 -04:00
.RE