workspaces
Select CLI Version:
See Details
Table of contents
Description
Workspaces 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 file system from within a singular top-level, root package.
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 ofnpm install
and avoiding manually having to usenpm link
in order to add references to packages that should be symlinked into the currentnode_modules
folder.
We also refer to these packages being auto-symlinked duringnpm install
as a singleworkspace, meaning it's a nested package within the current local file system that is explicitly defined in thepackage.json
workspaces
configuration.
Defining workspaces
Workspaces are usually defined via theworkspaces
property of thepackage.json
file, e.g:
{"name":"my-workspaces-powered-project","workspaces":["packages/a"]}
Given the abovepackage.json
example living at a current working directory.
that contains a folder namedpackages/a
that itself contains apackage.json
inside it, defining a Node.js package, e.g:
.+-- package.json`-- packages+-- a|`-- package.json
The expected result once runningnpm install
in this current working directory.
is that the folderpackages/a
will get symlinked to thenode_modules
folder of the current working dir.
Below is a postnpm install
example, given that same previous example structure of files and folders:
.+-- node_modules|`-- a ->../packages/a+-- package-lock.json+-- package.json`-- packages+-- a| `-- package.json
Getting started with workspaces
You may automate the required steps to define a new workspace usingnpm init. For example in a project that already has apackage.json
defined you can run:
npm init -w ./packages/a
This command will create the missing folders and a newpackage.json
file (if needed) while also making sure to properly configure the"workspaces"
property of your root projectpackage.json
.
Adding dependencies to a workspace
It's possible to directly add/remove/update dependencies of your workspaces using theworkspace
config.
For example, assuming the following structure:
.+-- package.json`-- packages+-- a|`-- package.json`-- b`-- package.json
If you want to add a dependency namedabbrev
from the registry as a dependency of your workspacea, you may use the workspace config to tell the npm installer that package should be added as a dependency of the provided workspace:
npm install abbrev -w a
Note: other installing commands such asuninstall
,ci
, etc will also respect the providedworkspace
configuration.
Using workspaces
Given thespecifities of how Node.js handles module resolution it's possible to consume any defined workspace by its declaredpackage.json
name
. Continuing from the example defined above, let's also create a Node.js script that will require the workspacea
example module, e.g:
// ./packages/a/index.jsmodule.exports='a'// ./lib/index.jsconst moduleA= require('a')console.log(moduleA) // -> a
When running it with:
node lib/index.js
This demonstrates how the nature ofnode_modules
resolution allows forworkspaces to enable a portable workflow for requiring eachworkspace in such a way that is also easy topublish these nested workspaces to be consumed elsewhere.
Running commands in the context of workspaces
You can use theworkspace
configuration option to run commands in the context of a configured workspace. Additionally, if your current directory is in a workspace, theworkspace
configuration is implicitly set, andprefix
is set to the root workspace.
Following is a quick example on how to use thenpm run
command in the context of nested workspaces. For a project containing multiple workspaces, e.g:
.+-- package.json`-- packages+-- a|`-- package.json`-- b`-- package.json
By running a command using theworkspace
option, it's possible to run the given command in the context of that specific workspace. e.g:
npm run test --workspace=a
You could also run the command within the workspace.
cd packages/a && npm run test
Either will run thetest
script defined within the./packages/a/package.json
file.
Please note that you can also specify this argument multiple times in the command-line in order to target multiple workspaces, e.g:
npm run test --workspace=a --workspace=b
It's also possible to use theworkspaces
(plural) configuration option to enable the same behavior but running that command in the context ofall configured workspaces. e.g:
npm run test --workspaces
Will run thetest
script in both./packages/a
and./packages/b
.
Commands will be run in each workspace in the order they appear in yourpackage.json
{"workspaces":["packages/a","packages/b"]}
Order of run is different with:
{"workspaces":["packages/b","packages/a"]}
Ignoring missing scripts
It is not required for all of the workspaces to implement scripts run with thenpm run
command.
By running the command with the--if-present
flag, npm will ignore workspaces missing target script.
npm run test --workspaces --if-present