Yarn Workspaces
Workspaces allow us to install dependencies from multiple package.json files at once.
- Yarn can also create symlinks between workspaces that depend on each other, ensuring directories are correct and consistent
- When there are repeated dependencies in the
node_modules
of sub-modules, yarn workspaces will pull up (hoist) the common dependencies to live in the root-levelnode_modules
.- This is why we don't need to run
lerna bootstrap --hoist
when using yarn + lerna — yarn already hoists.
- This is why we don't need to run
- if package1 depends on package2, then a symlink to package2 will also be created in the root
node_modules
. This allows package2 torequire
in the package, and take advantage of node's recursive resolver to find the package. - workspaces uses a single yarn.lock file at the root.
- any module (either your own code or the code of a dependency) that contains native code (ie. Swift, Java etc) must not be hoisted.
How to implement
- Add a
workspaces
key to the rootpackage.json
:
@app
holds all the modules
"workspaces": {
"packages": [
"@app/*"
]
}
- Change the name field of each module's
package.json
to follow a pattern so that they can be referenced easily - Add module script shortcuts to root
package.json
, using the module names as changed in the previous step:
"scripts": {
"db": "yarn workspace @tycholiz/db",
}
- Add
private: true
property to rootpackage.json
. - Add script in root
package.json
that allows us to execute multiple commands at once (note: consider usingconcurrently
package to run servers in parallel)
"scripts": {
"dev": "concurrently --kill-others-on-fail \"yarn sanity dev\" \"yarn web dev\"",
}
Children