Research is done by Marielle
Why? Because code sharing is the turbo boost for development speed. And when it comes to teaming up and collaborating, nothing beats the power of a monorepo — especially when armed with the right tools.
What is Lerna exactly?
Leveraging traditional tools like npm and Yarn, Lerna comprehends the intricate interconnections among packages within the git repository itself. Needless to say, this greatly simplifies the process of sharing code within the repository, making it effortlessly accessible.
Lerna has two modes of version management: fixed and independent. The fixed mode keeps all versions of packages at the same level. This approach is quite popular; you may have seen it in Angular:
The independent mode allows us to have different versions per package. If you have a large or complex project, using a single repository for all its packages can help with organization and maintenance.
The Lerna success is found in the thousands of teams worldwide who are using it, such as Jest, NestJS, Create React App, Strapi, WebdriverIO, and Nx.
Monorepos lets you develop multiple packages in the same repository, which is fantastic for productivity. You can share code, establish clear ownership, and have fast, unified Continuous Integration (CI). That is why more and more teams are switching to this way of development.
To use monorepos well, you need a good monorepo tool, and Lerna is exactly that. Lerna is fast, widely used, and battle-tested.
Benefits of Using Lerna Bootstrap
Using Lerna Bootstrap is a great way to get your project off the ground and running quickly. It sets up an initial mono repository structure for you, which you can tweak later on as needed.
Here are some other benefits of using the Lerna Bootstrap feature:
Never Duplicates Code Twice
Lerna intelligently prevents the execution of tasks that have been run before. Instead, it restores files and terminal output from its cache, ensuring efficiency and saving valuable development time. By adhering to this principle and leveraging the capabilities of Lerna, developers can streamline their workflow and maintain a clean and organized codebase.
Distributed cache is the ability to share computation cache between developers or CI/CD machines. This not only improves collaboration but also contributes to a significant reduction in average CI times. By leveraging this feature, teams can optimize their development workflows and achieve faster build and deployment processes, ultimately enhancing productivity and efficiency.
With Lerna, you can specify dependencies between different targets (npm scripts). So instead of managing dependencies and running them one by one, you can execute a single command to run all the dependent targets together. This saves you a lot of time, especially when you have a bunch of targets to handle.
You can unleash the full power of parallelization while maintaining the correct execution order with Lerna. Picture this: as many commands as possible running simultaneously, ensuring efficient and speedy execution. Say goodbye to command delays, especially when dealing with many other packages.
Infinite Scaling with Distributed Execution
Lerna empowers you to execute commands effortlessly across multiple machines, eliminating the need for complex configurations. Scale up Lerna to run on as many machines as you desire without the hassle of tedious setups. This exceptional capability accelerates command execution, making it particularly advantageous when publishing packages.
Powerful Graph Visualizer
Lerna offers a robust interactive workspace visualizer that enhances your understanding of your workspace architecture. This powerful tool provides valuable insights into the relationships between your packages, allowing you to identify and address any potential issues easily.
Lerna has this powerful dynamic CLI that ensures you only see what matters when you run the same process or commands across multiple projects. So, no more getting overwhelmed by a never-ending list of commands. You’ll only see the ones that actually matter for the project you’re working on.
Lerna boasts a seamless configuration process, keeping your files clutter-free. It enhances the execution of your npm scripts, delivering superior speed. As a result, you’ll save valuable time on the Lerna setup and dive right into utilizing its power efficiently.
Lerna Does it All
Why have separate tools for running tasks, linking packages, and then publishing packages to them? Lerna does it–less config, less overhead. Lerna is a single tool that can do everything you need to manage your monorepo, which can save you a lot of time and effort.
Drawbacks of Using Lerna
Despite all of its great features, there are some drawbacks to using Lerna. Here are a few things that you should be aware of before getting started with it:
Tests take a long time to run and lose syntax highlighting
The Lerna run test command goes through each package and runs the test script declared in package.json — the issue here is it will spawn a test runner for every single instance it finds. It also meant that our syntax highlighting (passing/failing/warnings etc.) was being omitted due to the test runs being spawned in a separate shell process.
Network calls for dependency resolution can become a bottleneck
In order to resolve the dependencies between packages, Lerna will make HTTP or file system calls. If you have a large number of packages, these network calls can add up and become a major bottleneck in your workflow.
NPM takes time to publish all related changes
When you are making changes to multiple packages, it can take a long time for all of the packages to be published with NPM. If you have a large number of packages or if one package has a lot of dependencies on other packages, this can add up and significantly slow down your workflow.
Lerna has a few core functionalities that make it one of the most popular monorepo tools out there. These include:
Lerna bootstraps all the packages in the main repo. In other words, it installs all the dependencies and links npm dependencies to any cross-dependencies, if there are any. It links all npm packages to the shared dependencies by creating symlinks. If any update is done in the shared dependency, it immediately takes effect in the code using it.
The publishing process has never been easier. All you have to do is enter a single command (Lerna publish), as it prompts for a new Lerna version and updates all the packages on git and npm. You can also force-publish a new package if you want to.
Lerna provides the ability to check which packages have been changed since the last release. You can also check the diff for all packages or a single package since the last release.
Running commands concurrently
Instead of moving to each project and then following commands after starting the server, you can write a script in the root package.json file, using the lerna run command to run the command in all the projects concurrently. This behavior is used when you’d like to run all the servers at the same time.
There are several other features that Lerna provides. These are just the basic ones. Have a look at their documentation for more information.
How to Use Lerna
We created an integration package and link header to integration using the following command as below:
npx lerna init
All Lerna functionality will work the same way regardless.
- add lerna to the root package.json
- generate a lerna.json
- configure a npm/yarn/pnpm workspace
Next, run the command below:
Now all the projects in the workspace can properly reference each other via local package linking.
Since Lerna is powered by Nx, you can use its capabilities to open an interactive visualization of the workspace project graph.
To open the visualization, run:
npx nx graph
We created an integration package and link header to integration using the command below:
lerna add header –scope=integration
Also, we tried to publish the packages in NPM:
Linked integration to remix app:
Outputs in frontend from different packages:
Lerna solves three of the biggest problems in monorepo. Check it out here. It is indeed a big help in the reusability of code, and we can achieve a new version with simplified dependency management and simplified package publishing.
If you plan to build a large application and think about architecting it into multiple projects, then you should definitely give Lerna a try. There’s a lot more Lerna can do.
Need Front-end and Back-end Services?
Your dream project may look impossible, but not to us! At StarTechUP, we offer software development services with an agile approach. Our experts can help in all areas of development from concept to launch.
Whether you need front-end services and back-end services, we can provide you with skilled developers who have experience in working with Lerna. You can also get our DevOps services to deploy and maintain your applications. Plus, our QA developers will ensure high-quality code!
Contact us to learn more about how our team of experienced engineers can assist you. We are excited to hear about your project and how we can help!