Nx VS Lerna

Preface
NX and Lerna are both excellent monorepo tools.
This article compares the differences between the two.
Before comparing, you can read these two articles
to understand the basic usage of NX and Lerna:
Task Execution
Both NX and Lerna can execute tasks.
In fact, Lerna’s task execution uses NX under the hood.
Single Task Execution
# nx
nx run is-odd:build
# lerna
lerna run build --scope=is-odd
Multi-task Execution
# nx
nx run-many --target=build
# lerna
lerna run build
Local Caching
Lerna does not have local caching by default.
NX provides local caching by default.
Enable Local Caching in NX
Just add an nx.json to the project:
{
"tasksRunnerOptions": {
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "test"]
}
}
}
}
Enable Local Caching in Lerna
-
Add an nx.json to the project as above
-
Or use the CLI command:
lerna add-caching
Distributed Caching
Distributed caching is a capability provided by nx-cloud.
Simply put: one person builds, the whole team gets the cache.

Enable Distributed Caching in NX
The following command binds the local accessToken to your nx-cloud account.
For detailed usage, see this article: https://blog.insistime.com/nx
nx connect-to-nx-cloud
Enable Distributed Caching in Lerna
Since Lerna is based on NX, enabling distributed caching in Lerna is the same as NX:
nx connect-to-nx-cloud
After running the command above, the runner in nx.json will be changed to nx-cloud,
and an accessToken will be generated:

Then you can visit the URL shown below to bind with nx-cloud:

View Dependencies
Lerna does not have this feature.
You can use NX’s capability directly.
View Package Dependencies with NX
nx graph
Version Management
Task execution, local caching, and distributed caching are primarily NX capabilities.
Lerna’s main strengths are version management and publishing.
NX does not have version management capability.
Version Management with Lerna
lerna version
The result looks like this:

Its power lies in:
-
Managing versions across multiple packages
-
Including dependency versions between packages
For example, if package A depends on package B, and B’s version changes, A’s version will be updated accordingly.
Publishing to npm
Similarly, NX does not have publish capability.
Lerna has publish capability.
Publishing Packages with Lerna
lerna publish
The result looks like this:

Lerna’s publish power lies in being able to publish multiple packages to npm simultaneously,
which is very useful for monorepos managing many published npm packages.
NX vs Lerna
Here’s a simple summary of the differences:

In fact, Lerna uses NX under the hood. Using Lerna with NX is the best approach:
# Lerna uses NX to enable local caching
lerna add-caching
# Lerna uses NX to enable distributed caching
nx connect-to-nx-cloud
# Lerna uses NX to view dependencies
nx graph