← Back to blog

Setting Up Monorepos in Yarn

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

  1. 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 mkdir command to do this:
bash
mkdir my-monorepo
cd my-monorepo
  1. Initialize a new Yarn project: This will create a package.json file, which will serve as the manifest file for your project. You can initialize a new project using the yarn init command:
bash
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.

  1. Modify the package.json file: Add a workspaces property to your package.json file. This property will contain an array of paths to your packages. For now, you can just include a generic path like "packages/*":
bash
{
"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.

  1. Create a packages directory: This will serve as the root directory for all your individual packages. You can use the mkdir command again:
bash
mkdir packages

Adding Packages

Now you can start adding packages to your monorepo.

  1. Navigate to the packages directory: Use the cd command to navigate to your packages directory:
plain
cd packages
  1. Create a new package: Use the yarn init command to create a new package:
bash
mkdir package-a
cd package-a
yarn 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.

  1. Navigate to the root directory of your monorepo: This is where your initial package.json file lives. You can use the cd command to get there:
bash
cd ..
cd ..
  1. Add a package to another package: You can add one package as a dependency to another using the yarn workspace command. For example, if you have a package named package-b and you want to use package-a inside it, you can run:
bash
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.

  1. Install the dependencies: You can install all of the dependencies for all of your packages at once by running yarn install in the root of your monorepo:
bash
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 ↗