|
| 1 | +--- |
| 2 | +id: docs_workspaces |
| 3 | +guide: docs_workspaces |
| 4 | +layout: guide |
| 5 | +--- |
| 6 | + |
| 7 | +{% include vars.html %} |
| 8 | + |
| 9 | +Workspace are a new way to setup your package architecture that's available by default starting from Yarn 1.0. It allows you to setup multiple packages in such a way that you only need to run `yarn install` once to install all of them in a single pass. |
| 10 | + |
| 11 | +### Why would you want to do this? |
| 12 | + |
| 13 | +- Your dependencies can be linked together, which means that a workspace can depend on another and always use the most up-to-date version of it, without having to run `yarn install` again like with the `file:` protocol, and with better safety mechanisms than with `yarn link`. |
| 14 | + |
| 15 | +- All your project dependencies will be installed together, which gives Yarn more latitude to better optimize them. |
| 16 | + |
| 17 | +- Yarn will use a single lockfile rather than a different one for each project. |
| 18 | + |
| 19 | +### How to use it? |
| 20 | + |
| 21 | +Add the following in a `package.json` file. Starting from now on, we'll call this directory the "workspace root": |
| 22 | + |
| 23 | +**package.json** |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "private": true, |
| 28 | + "workspaces": [ |
| 29 | + "workspace-a", |
| 30 | + "workspace-b" |
| 31 | + ] |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Then create two subfolders named `workspace-a` and `workspace-b`. In each of them, create another `package.json` file with the following content: |
| 36 | + |
| 37 | +**workspace-a/package.json:** |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "name": "workspace-a", |
| 42 | + "version": "1.0.0", |
| 43 | + |
| 44 | + "dependencies": { |
| 45 | + "cross-env": "5.0.5" |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +**workspace-b/package.json:** |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + "name": "workspace-b", |
| 55 | + "version": "1.0.0", |
| 56 | + |
| 57 | + "dependencies": { |
| 58 | + "cross-env": "5.0.5", |
| 59 | + "workspace-a": "1.0.0" |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Finally, run `yarn install` somewhere, ideally inside the workspace root. If everything works well, you should now have a similar file hierarchy: |
| 65 | + |
| 66 | +``` |
| 67 | +/package.json |
| 68 | +/yarn.lock |
| 69 | +
|
| 70 | +/node_modules |
| 71 | +/node_modules/cross-env |
| 72 | +/node_modules/workspace-a -> /workspace-a |
| 73 | +
|
| 74 | +/workspace-a/package.json |
| 75 | +/workspace-b/package.json |
| 76 | +``` |
| 77 | + |
| 78 | +And that's it! Requiring `workspace-a` from a file located in `workspace-b` will now use the exact code currently located inside your project rather than what is published on Github, and the `cross-env` package has been correctly deduped and put at the root of your project to be used by both `workspace-a` and `workspace-b`. |
| 79 | + |
| 80 | +### How does it compare to Lerna? |
| 81 | + |
| 82 | +Yarn's workspaces are the low-level primitives that tools like Lerna can (and [do](https://github.com/lerna/lerna/pull/899)!) use. They will never try to support the high-level feature that Lerna offers, but by implementing the core logic of the resolution and linking steps inside Yarn itself we hope to enable new usages and improve performances. |
| 83 | + |
| 84 | +### Tips & Tricks |
| 85 | + |
| 86 | + - The `workspaces` field is an array containing the paths to each workspace. Since it might be tedious to keep track of each of them, this field also accepts glob patterns! |
| 87 | + |
| 88 | +### Limitations & Caveheats |
| 89 | + |
| 90 | + - The package layout will be different between your workspace and what your users will get (the workspace dependencies will be hoisted higher into the filesystem hierarchy). Making assumptions about this layout was already hazardous since the hoisting process is not standardized, so theoretically nothing new here. |
| 91 | + |
| 92 | + - In the example above, if `workspace-b` depends on a different version than the one referenced in `workspace-a`'s package.json, the dependency will be installed from Github rather than linked from your local filesystem. This is because some packages actually need to use the previous versions in order to build the new ones (Babel is one of them). |
| 93 | + |
| 94 | + - Workspaces must be children of the workspace root in term of folder hierarchy. You cannot and must not reference a workspace that is located outside of this filesystem hierarchy. |
| 95 | + |
| 96 | + - Nested workspaces are not supported at this time. |
0 commit comments