40

Migrating from Lerna to Nx: Better Dev Ergonomics + Much Faster Build Times

 3 years ago
source link: https://blog.nrwl.io/migrating-from-lerna-to-nx-better-dev-ergonomics-much-faster-build-times-da76ff14ccbb
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.

Migrating from Lerna to Nx: Better Dev Ergonomics + Much Faster Build Times

Image for post
Image for post
Learn how to migrate from Lerna to Nx

In this post and video I’ll compare two dev tools for working with monorepos: Lerna and Nx.

Lerna offers several great affordances for developing multiple projects in the same repo, and it’s used by many open source projects. Nx is developed by former Googlers and provides a richer monorepo-based developer experience. Nx has a lot more in common with the tools you find at companies like Google, Uber, and Twitter.

In addition to comparing Lerna and Nx, I’ll show how to migrate your repository from Lerna to Nx.

https://youtu.be/Edv7ZeI8Bwc

The following is a summary of the video, plus a few extra notes.

Why Migrate From Lerna to Nx

Performance

Computation Caching

Nx implements computation caching. If someone on your team has already built or tested something, Nx can use their computation results to make the command you are running faster. This speeds up your local build/test time and also speeds up your CI (in some cases up to 10 times). And it works for any technology out of the box.

Code Analysis

Lerna is able to figure out what files your PR has changed and test/build only the projects affected by the changes. Nx goes a lot farther. It looks at the nature of the changes, which allows it to narrow down the list of affected projects.

Distributed Continuous Integration

Nx offers affordances for setting up distributed CI, which is a necessary step for any decent-sized repo. After all, even if you are smart with what you build, and cache what you’ve already built, once in a while a large change will affect the whole repo, and running CI on a single agent just won’t do.

Dev Ergonomics

VSCode Integration

Nx Console is a VS Code plugin for Nx. It’s great for inspecting workspaces. It uses a virtual file system to help with code generation, and more.

Image for post
Image for post

Github Integration

The free Nx Cloud Github app offers a much better way of reviewing CI runs.

Image for post
Image for post
Nx Cloud GitHub app build report
Image for post
Image for post
Nx Cloud run details

This is a great help for large repos with hundreds or thousands of projects, where making sense of the logs can get tricky. To learn more about run details and Nx GitHub integration, check out this blog post.

Ownership Management & Repository Constraints

Having no constraints and ad-hoc ownership management works well for open-source projects, but doesn’t work for organizations. Nx has the necessary capabilities to manage ownership and prevent the big ball of mud.

How to Migrate from Lerna to Nx

Step 1: Add Nx, Keep Existing Scripts

As shown in the Lerna to Nx migration video above, you can add a configuration file telling Nx where your projects are located and what you can do with them. This is enough to enable code analysis and computation caching.

Step 2: Use Nx Plugins

Nx is “a VS Code of build tools.” Even though its core functionality is great and useful, its real power is in its plugins. The video showed a very common issue in bespoke dev setups: the editor and execution experiences have to be handled differently. Nx plugins do it correctly. There are many other common issues (e.g., running jest tests in WebStorm or VSCode) that are handled correctly by Nx plugins and can really improve your quality of life.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK