6

Lerna is Officially Dead. Long Live Monorepos

 2 years ago
source link: https://betterprogramming.pub/lerna-is-officially-dead-long-live-monorepos-9853c80a7b0e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Lerna is Officially Dead. Long Live Monorepos

4 tools to manage efficiently your JavaScript monorepo

Caption by Author

The monorepo approach is a great way to manage all the dependencies. It has been a hot topic for some years. It is a great way to keep distinct projects with well-defined relationships. One immediate benefit is that it empowers us to share configurations like ESLint, Prettier, TypeScript, … through different projects ensuring consistency.

Tools like npm, yarn, and pnpm provide some native support for monorepo. However, they lack some features that were never intended to build. That’s where Lerna came into play. Its goal was to enhance and make it scalable helping build, orchestrate, and publish your packages.

Lerna has been officially killed recently. Although its creator @evocateur voiced his burnout and intention to abandon the project it was not made official until April 2020.

Important note: this project is not actively maintained. Consider adopting an alternative toolset for monorepo management. — Lerna’s Readme.md

The project won’t be handed over although it is consistently hitting a bit more than 1.3M daily downloads. Why? The author would be eager to rewrite it than make amends. It could not compete with the performance of the most recent tooling. Even if many still love and enjoy lerna it is time to move on.

So what are our choices when building a JavaScript/TypeScript monorepo ecosystem? In this article, we will be checking the most popular tooling.

1. Yarn, Npm, and Pnpm

As mentioned previously, npm, yarn, and pnpm already ship with native monorepo support. So you need to ask yourself: do you really need Lerna or any other tooling? Is your monorepo that complex?

The yarn one was the pioneer. It only arrived at npm in their release version 7 in October 2020. There are lots of similarities between these three.

In pnpm the workspace configuration lives in pnpm-workspace.yaml whilst in yarn and npm is it placed in root package.json.

Example using npm workspaces

To run commands like test we can use the --workspaces on npm and -r or recursive in pnpm.

One feature of yarn is that it lets you simplify the dependency process by dynamically replacing any workspace: reference. It is super convenient.

Which one is the best of those three? Probably I would pick yarn as it has the more mature feature although npm is catching up fast. The pnpm has other benefits like disk usage but this could be achieved with yarn berry.

When to use any of those three? Only on relatively small projects as it lacks the orchestration side of it. If you need complex dependencies graphs or releases better opt for another tooling.

2. Rush stack

Rush is a tool created by Microsoft aiming to solve the Monorepo management for JavaScript/TypeScript projects. It was born as an internal management tool and evolved.

What are its best features?

  • automated local linking
  • fast builds
  • Subset or incremental builds
  • Cyclic dependencies
  • Bulk publishing

It is super easy to install and get started, here is how to install it:

npm install -g @microsoft/rush

This tool offers a great degree of control over how best to structure your application. It is non-opinionated. It is a build and release orchestrator. It is built on top of any of the three top packager managers: yarn, pnpm, or npm. Out of those three npm is the most compatible one. The only caveat is that they suggest using it with the version 4.5.0 which is fairly old.

How does it work? It is all configured through the rush.json file. Let’s see an example of using the pnpm tool.

Let’s see how a project dependency projects are configured on that file:

It is still fairly consistent with the other tooling. The Rush tooling is well suited for large organizations that want to have granular control of their project npm dependencies.

3. Turborepo

Picture from https://github.com/vercel/turborepo

Vercel recently acquired the Turborepo company, taking control over its smart monorepo tool. It was created by the author of Formik and is built using the go language. That makes it excel at parallel execution.

What are its best features?

  • Computation and Remote caching
  • Parallel task execution
  • Easy to use and configure
  • Dependency visualization
  • Changes are continuously integrated

Getting started, we can just the trigger a terminal setup wizard by running

// execute
npx create-turbo@latest my-turbo-repo

Just like Rush, Turborepo requires selecting a package manager. The options are available: npm, pnpm, and yarn. As it integrates well with those we can transition easily to this tool if we are already using their native workspace features.

Note how we can manage package dependencies by using the "*" notation. This will keep the app with the most up-to-date version.

We may create build pipelines through a turbo.json. This is where we define how we want our projects to be built.

This tool is great if you want to have an unopinionated monorepo where you will like to deeply configure your building processes.

Although promising, it still has a long way to catch up with mature tooling like nx. With vercel behind it, it is likely to make great progress. It is a sure bet for the future.

Picture from https://github.com/nrwl/nx

The nx has been available for years but has become highly popular just recently. It is built using TypeScript. But don’t get fooled by this. It is extremely performant. Heavy computation is done by the core Node.js and modules written in C++.

Its philosophy is about having an opinionated structure of projects. They created a bit plugin ecosystem to keep the whole structure DRY, versatile, and pluggable. It is an extensible, fast, and smart build system.

What are its best features?

  • Smart rebuilds. It only rebuilds and retests what is affected by our changes
  • Project graph
  • Distributed task execution
  • Computation and Remote caching
  • Developer experience
  • Ownership management
  • Generators
  • Plugin Ecosystem
  • CL / VSCode extensions

Migrating is simple, first, add nx by executing the command

// execute
npx add-nx-to-monorepo

What does this do?

  • Adds Nx to your package.json.
  • Creates nx.json, containing all the necessary configuration
  • Sets up Nx Cloud (if you chose “yes”), which is free and does not require authentication.

What are the next steps?

  • Use plugins
  • Delete redundant configurations leveraged to plugins

The nx tool is solid and performant. It is more mature than turborepo and rush. There is great community support and more available resources. Its VSCode plugin makes it super intuitive to use its CLI. You can never go wrong by choosing it, it is a safe bet.

Wrap Up

Currently, the Lerna project has nearly the same downloads as nx and more than turborepo and rush combined. This is a trend that will be shifting as more people become aware of the lack of maintenance.

Although being quite new, turborepo has become one of my favorites so far. It seems to check all of the boxes. It goes without saying that you nx. It is mature, fast, and reliable.

Cheers.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK