Monorepos, short for monolithic repositories, are a type of software repository structure where all codebase related to a project is stored in a single repository. They provide a consolidated, easy-to-navigate structure, and facilitate code sharing and reusability across different projects.
The steps to setting up a monorepo in Yarn are relatively straightforward, but require some familiarity with Yarn and its command line interface. This guide assumes you have Yarn installed and have a basic understanding of its operations.
Initial Setup
- Create a new directory for the monorepo: First, you need to create a directory where all of your packages will live. You can use the
mkdircommand to do this:
mkdir my-monorepocd my-monorepo
- Initialize a new Yarn project: This will create a
package.jsonfile, which will serve as the manifest file for your project. You can initialize a new project using theyarn initcommand:
yarn init -y
The -y flag will automatically fill in the defaults for you.
Configuring Yarn Workspaces
Now you need to configure Yarn to recognize your project as a monorepo.
- Modify the
package.jsonfile: Add aworkspacesproperty to yourpackage.jsonfile. This property will contain an array of paths to your packages. For now, you can just include a generic path like"packages/*":
{"name": "my-monorepo","private": true,"workspaces": ["packages/*"]}
The "private": true line is necessary because Yarn doesn't allow you to publish a package that contains other packages.
- Create a packages directory: This will serve as the root directory for all your individual packages. You can use the
mkdircommand again:
mkdir packages
Adding Packages
Now you can start adding packages to your monorepo.
- Navigate to the packages directory: Use the
cdcommand to navigate to your packages directory:
cd packages
- Create a new package: Use the
yarn initcommand to create a new package:
mkdir package-acd package-ayarn init -y
This will create a new package.json file for your package. You can repeat this process for as many packages as you need.
Using the Packages
Now you can start using your packages.
- Navigate to the root directory of your monorepo: This is where your initial
package.jsonfile lives. You can use thecdcommand to get there:
cd ..cd ..
- Add a package to another package: You can add one package as a dependency to another using the
yarn workspacecommand. For example, if you have a package namedpackage-band you want to usepackage-ainside it, you can run:
yarn workspace package-b add package-a
Yarn will automatically link the two packages together. Any changes you make to package-a will be immediately available in package-b.
- Install the dependencies: You can install all of the dependencies for all of your packages at once by running
yarn installin the root of your monorepo:
yarn install
Yarn will create a single yarn.lock file in the root of your monorepo. This file will contain the exact versions of all the dependencies of all your packages.
And that's it! You have now set up a basic monorepo using Yarn. From here, you can start adding code to your packages, creating scripts to run your packages, and more. As your project grows, you'll find that managing your packages in a monorepo provides a number of benefits, including simplified dependency management, improved code sharing and collaboration, and more.
Enjoyed this post? Read the next one ↗