Lerna – The key of Monorepo Management

Khushi Patel - Sep 12 - - Dev Community

Welcome Back to Monorepo Castle!
The castle is now built, and every room (project) is in place. But without the right management, things could get messy. Who will help the castle run smoothly? That’s when Lerna enters—a powerful wizard with magical commands that keep everything in order.

Lerna is your guide in the land of monorepo, making sure all the rooms (projects) are in sync, all the packages are linked, and nothing falls apart.

Meet the Magical Wizard: Lerna

Lerna isn't just any wizard—it’s a super wizard designed to make your life easier while managing multiple projects in one repo. Let’s take a peek at some of Lerna’s spells (commands):

1. lerna init – The “Let’s Build a Castle” Spell
This is where it all starts. You summon Lerna with this command, and it sets up the structure for your monorepo. It’s like laying the foundation for your castle—each room has its place.

lerna init
Enter fullscreen mode Exit fullscreen mode

Then define your workspaces in package.json. With Yarn or pnpm, you can list all the packages that should be managed:


{
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}
Enter fullscreen mode Exit fullscreen mode

This tells the package manager to treat everything inside the packages/ directory as individual sub-projects (frontend, backend, shared libraries, etc.).

2. Yarn or pnpm for Dependency Management
Previously, Lerna had lerna bootstrap to link dependencies across packages, but this has been deprecated. Instead, use Yarn or pnpm for this task. you can run any of bellow command

yarn install
pnpm install
Enter fullscreen mode Exit fullscreen mode

This automatically links all the projects inside your monorepo, so they can share dependencies. It also deduplicates packages to reduce size, making the installation process faster.

3. Running Commands Across Packages
One of the most powerful features of Lerna is still intact: running scripts across multiple packages in the monorepo. If your monorepo has both a frontend and backend, for example, you can run build or test commands across both without switching directories.

Run the build script in every package:

lerna run build
Enter fullscreen mode Exit fullscreen mode

Lerna will go into each package and execute the build script, which is extremely useful when you want to ensure all packages are in sync before deployment.

4. Versioning & Publishing
Lerna’s original focus remains unchanged: versioning and publishing.
Versioning: Lerna will automatically bump versions for your packages based on the changes you’ve made. This ensures that when you make updates, version numbers are consistent across the whole project.

lerna version
Enter fullscreen mode Exit fullscreen mode

Publishing: When it’s time to release, you can use Lerna to publish all packages at once, or selectively, depending on your needs:

lerna publish
Enter fullscreen mode Exit fullscreen mode

With Lerna by your side, managing a monorepo becomes an absolute easy. From syncing dependencies to running scripts across projects, Lerna keeps the castle organized, efficient, and fast.

Stay tuned for more wizardry as we explore other tools in this magical land of web development. ✨

till then, Happy Coding ! 🫶🏻

. . . . . . . . . . . . . . . .