21 Oct 2024

What, Why, and How to Get Started

What is a Monorepo?

So, you've got a bunch of different projects going on, and instead of having a separate repository for each one, you put all the code in a single spot - that's basically what a monorepo is. It's a way of storing code for multiple projects in one place, which is a bit different from the usual way of doing things, where each project or component has its own repository.

In a monorepo, you might find:

  • Multiple related projects

  • Shared libraries or components

  • Documentation

  • Build scripts and configuration files

  • Test suites

All of these coexist within a single version control system, allowing for easier management and coordination of changes across the entire codebase.

Why Use a Monorepo?

Advantages:

  1. Simplified dependency management: Shared dependencies are easier to manage and update across all projects.

  2. Code reuse: It's easier to share and reuse code between projects when everything is in one place.

  3. Atomic commits: Changes that span multiple projects can be committed together, ensuring consistency.

  4. Easier refactoring: Large-scale refactoring becomes more manageable when all affected code is in one repository.

  5. Simplified CI/CD: Build and deployment processes can be standardized across all projects.

  6. Better visibility: Developers can see and understand the entire codebase, fostering collaboration.

Disadvantages:

  1. Increased repository size: As all projects are in one place, the repository can become very large over time.

  2. Slower version control operations: Large repositories can lead to slower cloning, pulling, and pushing.

  3. Access control challenges: It can be more difficult to restrict access to specific parts of the codebase.

  4. Tooling complexity: Some tools may not be optimized for monorepo structures, requiring custom configurations.

  5. Potential for tight coupling: There's a risk of creating unnecessary dependencies between projects if not carefully managed.

When to Consider a Monorepo

Monorepos can be particularly beneficial in the following scenarios:

  • When working on a large, complex application with multiple interconnected components

  • For organizations with many related projects that share common dependencies or utilities

  • In situations where frequent cross-project changes are necessary

  • When standardization of tooling and processes across projects is a priority

Getting Started with Monorepos: Using Yarn Workspaces

Yarn Workspaces is a feature of the Yarn package manager that makes it easy to set up and manage a monorepo. Here's a step-by-step guide to creating your first monorepo using Yarn Workspaces:

Initialize your monorepo: Create a new directory for your monorepo and initialize it with Yarn:

mkdir my-monorepo
cd my-monorepo
yarn init -y

Enable Yarn Workspaces: Edit the package.json file in the root directory to enable workspaces:

{
  "name": "my-monorepo",
  "private": true,
  "workspaces": ["packages/*"]
}

Create your first packages: Make a packages directory and create subdirectories for each of your projects:

mkdir -p packages/project-a packages/project-b

Initialize each package: Navigate to each project directory and initialize it:

cd packages/project-a
yarn init -y
cd ../project-b
yarn init -y

Add dependencies: You can add dependencies to individual projects or shared dependencies at the root:

# Add a dependency to project-a
cd packages/project-a
yarn add lodash

# Add a dev dependency to all projects
cd ../..
yarn add -W -D jest

Create a script to run all projects: In the root package.json, add a script to run all projects:

{
  "scripts": {
    "start": "yarn workspaces run start"
  }
}

Use shared code: You can create shared modules in one package and use them in another:

// In packages/project-a/index.js
module.exports = {
  sayHello: () => console.log("Hello from Project A!")
};

// In packages/project-b/index.js
const projectA = require('project-a');
projectA.sayHello();

Run your monorepo: From the root directory, you can now run:

yarn start

This setup provides a basic structure for your monorepo. As your project grows, you may need to add more complex configurations for building, testing, and deploying your packages.

So, monorepos have their perks, but they also bring some challenges to the table. Before jumping in, take a close look at what your project needs and how your team works to decide if a monorepo is the way to go.

© 2024, Priyansh Rastogi.

Powered by Discoverable