What is New in Lerna v6 with Nx?

WESEEK, Inc.
5 min readFeb 13, 2024

--

Introduction

Hello, I’m Takoi, a System Engineer from WESEEK, Inc. in Japan.
In this article, I will explain about:
· What is Lerna?
· Changes in Lerna v5
· Changes in Lerna v6
· Cache Task Results
· Share Cache with nx cloud

What is Lerna?

Lerna is a monorepo tool.
Monorepo refers to the management of multiple services in a single repository.

The advantages of monorepo include, for example, the following:

  • Code can be shared among multiple services in a repository.
  • Cross-service testing
  • Easier to manage releases
  • Ability to use tools such as lint with common rules for each service

Lerna can be used to manage multiple node packages at once.

By using the lerna bootstrap command, packages can be installed in batches based on the package.json of each service, and the same type and version of packages can be used in common.

Lerna, which Evocateur developed, stopped development at version 4.0.0, but Nrwl, the developer of Nx, a monorepo tool, has taken over the development and released v5 and v6.

Changes in Lerna v5

With Lerna v5, Lerna now has a cache function.

Enabling the useNx flag in lerna.json will make it as fast as Nx, and depending on the repository configuration, you can expect a 2–10x speedup compared to the previous Lerna.

Reference: https://blog.nrwl.io/lerna-used-to-walk-now-it-can-fly-eab7a0fe7700

Changes in Lerna v6

In Lerna v6, useNx, introduced earlier, is enabled by default.

You can also run lerna add-cachingto generate nx.json, which describes the configuration of Nx features.

You can specify script dependencies in nx.json.

For example, if nx.json is set as above, Lerna will execute the build for all dependent packages when either build or dev scripts are executed.

Also, scripts set to cacheableOperations, described in the next section, will have their execution results cached.

Reference: https://blog.nrwl.io/lerna-reborn-whats-new-in-v6-10aec6e9091c

Cache Task Results

For example, if nx.json is configured as above, the results of task runner executions (e.g. lerna run lint) will be cached.
The target scripts must be registered in cacheableOperations.

Let’s run lerna run lint twice as a test.

The first run took about 25 seconds.
The second run showed the message Nx read the output from the cache instead of running the command for 2 out of 2 tasks.The execution time is now only about 2 seconds.

Nx checks each package to see if there are any differences in the files since the last run and uses the cache if the files have not changed.

The cache is stored locally for one week. The locally stored cache can be deleted by executing the nx reset command.

Note that cacheableOperations should only be set to scripts that always output the same result for the same input.

Also, files generated by command execution (e.g., creation of dist directories) can be completed immediately by making the appropriate settings in nx.json.

Reference: https://lerna.js.org/docs/features/cache-tasks

Share Cache with nx cloud

The cache can be shared among multiple machines by using nx cloud. You can connect your workspace to nx cloud simply by doing this:

When the execution completes, an access token is added tonx.json, and the cache is stored on nx cloud from then on.

After running lerna run lint once as a trial, delete the local cache with the nx reset command.

Then run yarn lerna run lint again.

We have confirmed that the cache stored in nx cloud is used and the task is completed immediately.

You can also view the results of the task execution which appears in the log when the task is executed.

Reference: https://lerna.js.org/docs/features/share-your-cache

Summary

In this article, I explained the new Nx feature added to Lerna.

I was surprised at how easy it is to store and use caches in the cloud for free. It is very useful, so please have a try.

More Tech Articles from us:
Speech Recognition with Python & CMU Sphinx
A Quick Guide for GraphQL
Quickly Trace and Understand The Key Concepts of VictoriaMetrics
React Native: useId To Prevent Duplicate ID

About Us💡

In addition, we want to introduce a little more about GROWI, an open software developed by WESEEK, Inc.

GROWI is a wiki service with features-rich support for efficient information storage within the company. It also boasts high security and various authentication methods are available to simplify authentication management, including LDAP/OAuth/SAML.

GROWI originated in Japan and GROWI OSS is FREE for anyone to download and use in English.

For more information, go to GROWI.org to learn more about us. You can also follow our Facebook to see updates about our service.

GROWI.org

--

--

WESEEK, Inc.

WESEEK, Inc, a team of skilled system development professionals. Our blog shares insights on our online wiki service "GROWI.cloud" and emerging IT knowledges.