Skip to content

Commit 03fca6a

Browse files
ruyadornoisaacs
authored andcommitted
docs: workspaces
Adds docs on workspaces, explaining its basic concept and how to use it.
1 parent 06bd0e0 commit 03fca6a

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

docs/content/cli-commands/npm-install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the
3434
installation of dependencies will be driven by that, respecting the following
3535
order of precedence:
3636

37-
* `node_modules/.package-lock.json`
3837
* `npm-shrinkwrap.json`
3938
* `package-lock.json`
4039
* `yarn.lock`
@@ -495,3 +494,4 @@ specific folder structures that npm creates.
495494
* [npm uninstall](/cli-commands/uninstall)
496495
* [npm shrinkwrap](/cli-commands/shrinkwrap)
497496
* [package.json](/configuring-npm/package-json)
497+
* [workspaces](/using-npm/workspaces)

docs/content/configuring-npm/package-json.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,31 @@ probably matter for the purposes of publishing.
886886
See [`config`](/using-npm/config) to see the list of config options that can be
887887
overridden.
888888
889+
### workspaces
890+
891+
The optional `workspaces` field is an array of file patterns that describes
892+
locations within the local file system that the install client should look up
893+
to find each [workspace](/using-npm/workspaces) that needs to be symlinked to
894+
the top level `node_modules` folder.
895+
896+
It can describe either the direct paths of the folders to be used as
897+
workspaces or it can define globs that will resolve to these same folders.
898+
899+
In the following example, all folders located inside the folder `./packages`
900+
will be treated as workspaces as long as they have valid `package.json` files
901+
inside them:
902+
903+
```json
904+
{
905+
"name": "workspace-example",
906+
"workspaces": [
907+
"./packages/*"
908+
]
909+
}
910+
```
911+
912+
See [`workspaces`](/using-npm/workspaces) for more examples.
913+
889914
### DEFAULT VALUES
890915
891916
npm will default some values based on package contents.
@@ -910,6 +935,7 @@ npm will default some values based on package contents.
910935
### SEE ALSO
911936
912937
* [semver](/using-npm/semver)
938+
* [workspaces](/using-npm/workspaces)
913939
* [npm init](/cli-commands/init)
914940
* [npm version](/cli-commands/version)
915941
* [npm config](/cli-commands/config)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
section: using-npm
3+
title: workspaces
4+
description: Working with workspaces
5+
---
6+
# scope(7)
7+
8+
## Workspaces
9+
10+
### Description
11+
12+
**Workspaces** is a generic term that refers to the set of features in the
13+
npm cli that provides support to managing multiple packages from your local
14+
files system from within a singular top-level, root package.
15+
16+
This set of features makes up for a much more streamlined workflow handling
17+
linked packages from the local file system. Automating the linking process
18+
as part of `npm install` and avoiding manually having to use `npm link` in
19+
order to add references to packages that should be symlinked into the current
20+
`node_modules` folder.
21+
22+
We also refer to these packages being auto-symlinked during `npm install` as a
23+
single **workspace**, meaning it's a nested package within the current local
24+
file system that is explicitly defined in the [`package.json`](/using-npm/package-json)
25+
`workspaces` configuration.
26+
27+
### Installing workspaces
28+
29+
Workspaces are usually defined via the `workspaces` property of the
30+
[`package.json`](/using-npm/package-json) file, e.g:
31+
32+
```json
33+
{
34+
"name": "my-workspaces-powered-project",
35+
"workspaces": [
36+
"workspace-a"
37+
]
38+
}
39+
```
40+
41+
Given the above `package.json` example living at a current working
42+
directory `.` that contains a folder named `workspace-a` that disposes
43+
of a `package.json` inside it, defining a nodejs package, e.g:
44+
45+
```
46+
.
47+
+-- package.json
48+
`-- workspace-a
49+
`-- package.json
50+
```
51+
52+
The expected result once running `npm install` in this current working
53+
directory `.` is that the folder `workspace-a` will get symlinked to the
54+
`node_modules` folder of the current working dir.
55+
56+
Below is a post `npm install` example, given that same previous example
57+
structure of files and folders:
58+
59+
```
60+
.
61+
+-- node_modules
62+
| `-- workspace-a -> ../workspace-a
63+
+-- package-lock.json
64+
+-- package.json
65+
`-- workspace-a
66+
`-- package.json
67+
```
68+
69+
### Using workspaces
70+
71+
Given the [specifities 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
72+
by it's declared `package.json` `name`. Continuing from the example defined
73+
above, let's also create a Node.js script that will require the `workspace-a`
74+
example module, e.g:
75+
76+
```
77+
// ./workspace-a/index.js
78+
module.exports = 'a'
79+
80+
// ./lib/index.js
81+
const moduleA = require('workspace-a')
82+
console.log(moduleA) // -> a
83+
```
84+
85+
When running it with:
86+
87+
`node lib/index.js`
88+
89+
This demonstrates how the nature of `node_modules` resolution allows for
90+
**workspaces** to enable a portable workflow for requiring each **workspace**
91+
in such a way that is also easy to [publish](/cli-commands/publish) these
92+
nested workspaces to be consumed elsewhere.
93+
94+
### See also
95+
96+
* [npm install](/cli-commands/install)
97+
* [npm publish](/cli-commands/publish)
98+

0 commit comments

Comments
 (0)