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