Mastering Monorepos with Lerna

Monorepos have become a popular architectural approach for managing multiple projects with well-defined relationships. However, they can be challenging to manage, especially in larger projects. This is where tools like Lerna come in – to alleviate these challenges and make monorepos a more appealing option.

Why Use a Monorepo?

Using a monorepo provides better dependency management and code sharing between projects. It also enables fast and unified CI, making it possible to develop multiple packages in the same repository and share configurations like Prettier and ESLint throughout your different projects.

The Importance of Tools like Lerna

Publishing individual packages and sequential builds can take a long time, making monorepos difficult to manage. Tools like pnpm, Yarn, or npm can help, but they lack some vital features that you can only get with a tool like Lerna. Lerna modularizes the code into smaller, more manageable packages and abstracts out shareable code, making monorepos more scalable.

Lerna’s New Era

Lerna was among the first monorepo tools but was considered obsolete in April 2022. However, Nrwl, the company behind Nx, took over Lerna and gave it a new life. The new Lerna has received several notable upgrades, including integration with Nx, a brand new website, updated documentation, and improved performance.

Integration with Nx

In Lerna v6, you can integrate with Nx, which provides features like smart rebuilds, caching capabilities, project graph, distributed task execution, computation, and remote caching. Nx also improves the developer experience with a beautiful terminal output that makes it easier to see what has failed and why.

Remote Caching via Nx Cloud

Remote caching is an improvement Lerna gets from adopting Nx as its task scheduler. It allows you to distribute your local cache with the rest of the team and the CI system via Nx Cloud. You can also visualize your CI run and distribute project tasks dynamically across several machines.

Target Dependencies: Defining a Task Pipeline

When running tasks in a monorepo, you need to account for potential dependencies among different tasks. In Lerna v6, you can specify these dependencies in the nx.json file by defining the targetDefaults.

pnpm Support

Lerna now supports pnpm workspaces, making it possible to utilize the best qualities of both tools. When used in a pnpm workspace, Lerna will resolve package locations with pnpm-workspace.yaml and restrict the use of Bootstrap and link.

Plugin Ecosystem: VS Code Extension

Lerna provides a VS Code extension that helps navigate the monorepo. With the extension installed, you can run commands directly by right-clicking on the project and visualize the project and its relationships.

Prettier Support

Lerna v6 auto-detects if Prettier is set up in the workspace and applies the formats set to all files in the project when running the lerna version command.

Dynamic Terminal Output

The new Lerna displays only the most relevant output at any given moment, making it easier to keep track of what tasks were built and which tasks failed.

lerna repair

The lerna repair command ensures that any configuration file changes for the latest version of Lerna are applied, keeping the application from breaking and reducing the possibility of errors.

Lerna has undergone significant improvements, making it a more appealing option for managing monorepos. Its integration with Nx has resulted in notable changes, including caching by default and remote caching. The addition of features like dynamic terminal output and the plugin ecosystem improve the user experience and simplify the workspace.

Leave a Reply

Your email address will not be published. Required fields are marked *