ENEscrito em inglês6 min de leitura
How to Create Monorepos with Yarn Workspaces
What monorepos are, why they help, and how to set one up with Yarn workspaces, from sharing code between packages to publishing them.
Hey there! Have you ever heard of monorepos? They're a super useful approach in software development that can make your life as a developer much easier. Let me break it down for you in simple terms.

TL;DR: move to Let's create our workspaces if you are only interested in how to create monorepos.
A monorepo, short for "monolithic repository," is a way of organizing your code where you keep multiple projects or applications in a single version control repository. Instead of having each project in its own separate repository, you put them all together in one big happy family.
Now, you might be wondering, why would you want to do that? Well, there are several benefits to using a monorepo:
- Code Sharing and Reusability: Developers can easily share common libraries, components, and utilities across different projects, reducing duplication and improving overall code quality.
- Simplified Dependency Management: Monorepos allow for centralized dependency management, making it easier to track and update dependencies across all projects simultaneously (Here's where Yarn workspaces come into play).
- Streamlined Continuous Integration and Deployment: CI/CD processes are simplified in a monorepo environment. Changes made to any project can trigger automated tests and deployment pipelines for all projects, ensuring a consistent and reliable release process.
- Easier Refactoring and Code Maintenance: Developers can make sweeping changes across the entire codebase more efficiently, promoting cleaner code and architectural improvements.
- Consistent Coding Standards and Best Practices: Enforcing coding standards and best practices becomes easier in a monorepo, as all projects are subject to the same rules and guidelines. This can lead to a more consistent and maintainable codebase.
- Facilitates Large-Scale Projects: Monorepos are particularly beneficial for large-scale projects with multiple interdependent components. It provides a unified view of the entire project, making it easier to manage and coordinate efforts across the organization.
Yarn is a popular package manager for JavaScript, and
workspaces are a feature it provides that makes monorepo management a piece of cake.
It helps you link dependencies, run commands across all projects, and overall simplifies working with multiple projects in one repository.
So, if you're interested in learning how to create a monorepo using yarn workspaces, I'd be more than happy to guide you through the process! It's a fantastic way to streamline your development workflow and boost collaboration within your team.
Let's create our workspaces
First, some vocabulary
In the context of the workspace feature, a project is the whole directory tree making up your workspaces (often the repository itself).
A workspace is a local package made up from your own sources from that same project.
Finally, a worktree is the name given to workspaces that list their own child workspaces.
A project contains one or more worktrees, which may themselves contain any number of workspaces. Any project contains at least one workspace: the root one.
What will happen to your packages? (e.g. node_modules folder)
When using Yarn workspaces, all the dependencies listed in each workspace will be installed in the root directory, and the workspaces will retrieve information from there instead of retrieving from their local node_modules or cache.
If you have some configuration that must specify a package's location (such as React Native Android build.gradle), be aware to use the right path.
Edit your package.json
In your package.json, add a new field named workspaces like this
{
...
"workspaces": [
"packages/*"
],
}Create a packages folder
In the root of your project create a packages folder and inside it, create all the workspaces for your project. Each one of these workspaces must have a package.json file to define it as a workspace.
my-project/
├── packages/
│ ├── main/
│ │ └── package.json
│ │
│ ├── auth/
│ │ └── package.json
│ │
│ └── components/
│ └── package.json
│
└── package.jsonInside each workspace's package.json describe its name and version.
{
"name": "@my-project/auth",
"version": "1.0.0",
}Now you should be able to develop each workspace separately, but still leverage shared code from other workspaces.
How to use code from other workspace
Say we want to use @my-project/auth code inside @my-project/main, we'll have to declare @my-project/auth as a dependency.
{
"name": "@my-project/main",
"version": "1.0.0",
"dependencies": {
"@my-project/auth": "workspace:*"
}
}We use the workspace: range. With this, we tell our package manager to use the version of the package available in its workspace inside our project, not from other sources (such as npm). This way, we have always up-to-date code from packages listed inside our project.
Note that workspace: acts like any other dependency in package.json. You can use it with any ranges (~, ^, ^1.2.3, etc).
Adding dependencies and running commands to a specific workspace
To deal with specific workspaces in your project you don't need to cd into the desired workspace and run a command, you can do this by running yarn workspace command. For example
yarn workspace @my-project/components add -D reactWill add react as a dependency in the components workspace.
You can use this to run any yarn command inside a workspace
yarn workspace @my-project/components run buildRemember that you must use the name listed in the workspace's package.json attribute.
How to run a command for each workspace
First, you must install workspace's plugin
yarn plugin import workspace-toolsThen, you may be able to run the yarn workspaces foreach command that runs the desired command inside each workspace of your project. For example, this will add React as a dependency in all our three workspaces.
yarn workspaces foreach add reactPublishing your packages
This section is only important if you plan to publish your package (e.g. publish in npm).
When publishing, yarn will transform your package.json file depending on the ranges you set to each workspace: dependency.
So for example, if we assume we have the following workspaces whose current version is 1.5.0, the following:
{
"dependencies": {
"star": "workspace:*",
"caret": "workspace:^",
"tilde": "workspace:~",
"range": "workspace:^1.2.3",
"path": "workspace:path/to/baz"
}
}Will be transformed into:
{
"dependencies": {
"star": "1.5.0",
"caret": "^1.5.0",
"tilde": "~1.5.0",
"range": "^1.2.3",
"path": "1.5.0"
}
}Further reading…
That's all! Hope you enjoyed that article! Here are some other link to check and improve your knowledge:
- Workspaces | Yarn — Package Manager (yarnpkg.com) (abre em uma nova aba)
yarn workspace| Yarn — Package Manager (yarnpkg.com) (abre em uma nova aba)
Originally published on JavaScript in Plain English (abre em uma nova aba).