Monorepos, short for “monolithic repositories,” has gained popularity in the software development world for managing large-scale projects efficiently. One tool that stands out in the world of monorepos is Lerna. In this blog, Let’s delve into unleashing the power of Monorepos with Lerna, how to use it, and explore its pros and cons.
1. What is Lerna?
Lerna is a tool for managing JavaScript projects with multiple packages. It optimizes the workflow around multi-package repositories with git and npm. By optimizing the workflow, Lerna makes it easier to maintain and manage large-scale projects with multiple packages or modules.
Installation:
Before diving into usage, let’s install Lerna globally using npm:
npm install -g lerna
2. Setting Up a Monorepo with Lerna
Lerna works seamlessly with a monorepo structure. Let’s set up a simple example with two packages – package-a
and package-b
.
- Initializing Lerna:
lerna init
This command initializes a new Lerna repository, creating a packages
folder and a basic configuration file.
- Creating Packages:
lerna create package-a
lerna create package-b
This command creates two packages, package-a
and package-b
, within the packages
directory.
- Linking Packages:
lerna link
Lerna’s link
command establishes symlinks between the packages, allowing for easy cross-package development.
Note: The “link” command was removed by default in v7, and is no longer maintained.
- Installing Dependencies:
lerna bootstrap
The bootstrap
command installs all dependencies and links any cross-dependencies in the monorepo.
Note: The “bootstrap” command was removed by default in v7, and is no longer maintained.
3. Lerna in Action:
Let’s explore some common Lerna commands:
lerna exec
: Run a command in each package.
lerna exec -- ls
Result:
lerna run
: Run a script in each package.
lerna run test
Result:
lerna add
: Add a dependency to packages.
lerna add some-package
4. Pros and Cons of Lerna:
Pros:
Pros | Details |
---|---|
Simplified Management | Lerna simplifies the management of large-scale projects by optimizing workflows and providing tools for versioning and publishing. |
Cross-Package Development | Easy linking and testing between packages make cross-package development a breeze. |
Consistent Versioning | Lerna handles versioning across packages, ensuring consistency in version numbers. |
Enhanced Tooling Integration | Integration with popular build tools and CI/CD systems enhances the overall development workflow. |
Cons:
Cons | Details |
---|---|
Learning Curve | Transitioning to Lerna may require some learning, especially for teams new to monorepo structures. |
Potential Overhead | In smaller projects, Lerna may introduce unnecessary complexity and overhead. |
Setup Time | Setting up Lerna and configuring the monorepo structure can take time initially. |
5. List of commands:
Commands | Description |
---|---|
lerna add-caching | Interactive prompt to generate task runner configuration. |
lerna changed | List local packages that have changed since the last tagged release. [aliases: updated] |
lerna clean | Remove the node_modules directory from all packages. |
lerna clean -y | Removes the node_modules directory without asking for confirmation. |
| Creates a new package within the packages directory. |
lerna diff [from] [to] | Show what has changed in the repository since the last release. |
lerna diff [pkgName] | Diff all packages or a single package since the last release. |
lerna diff --json | Show what has changed in the repository since the last release in JSON format. |
lerna exec [ | Runs a command in each package. |
| Import a package into the monorepo with commit history. |
lerna info | Displays information about the Lerna version, configuration, and packages. |
lerna init | Create a new Lerna repo or upgrade an existing repo to the current version of Lerna. |
lerna init --independent | Initializes a new Lerna repository with independent versioning. |
lerna list | List local packages [aliases: ls, la, ll]. |
lerna publish [bump] | Publish packages in the current project. |
lerna repair | Runs automated migrations to repair the state of a lerna repo. |
lerna run <script> | Run an npm script in each package that contains that script. |
lerna watch | Runs a command whenever packages or their dependents change. |
lerna version [bump] | Bump version of packages changed since the last release. |
| Adds a dependency to all packages or a specific package. (Deprecated) The “add” command was removed by default in v7, and is no longer maintained. |
lerna bootstrap | Installs all dependencies and links any cross-dependencies. (Deprecated) The “bootstrap” command was removed by default in v7, and is no longer maintained. |
lerna link | Establishes symlinks between packages for local development. (Deprecated) The “link” command was removed by default in v7, and is no longer maintained. |
lerna link convert | Convert the packages directory to use symlinks. |
Global Options | Description |
---|---|
--loglevel | What level of logs to report. [string] [default: info] |
--concurrency | How many processes to use when lerna parallelizes tasks. [number] [default: 10] |
--reject-cycles | Fail if a cycle is detected among dependencies. [boolean] |
--no-progress | Disable progress bars. (Always off in CI) [boolean] |
--no-sort | Do not sort packages topologically (dependencies before dependents). [boolean] |
--max-buffer | Set max-buffer (in bytes) for subcommand execution [number] |
-h, --help | Show help [boolean] |
-v, --version | Show version number [boolean] |
6. When to Use Lerna:
- Large-Scale Projects: Lerna is particularly beneficial for managing large-scale projects with multiple packages or modules.
- Cross-Package Development: When there is a need for efficient linking and development across packages.
- Consistent Versioning: Projects that require consistent versioning across packages.
Use cases:
- Monorepo Management:
- Use Case: You have a large-scale project with multiple packages or modules that share common code and dependencies.
- How Lerna Helps: Lerna simplifies the management of a monorepo, allowing you to version, test, and publish packages together or independently.
- Cross-Package Development:
- Use Case: You need to work on multiple packages simultaneously and test changes across packages during development.
- How Lerna Helps: Lerna enables easy linking and testing between packages, streamlining the development process and improving collaboration.
- Consistent Versioning:
- Use Case: You want to ensure consistent versioning across all packages to maintain compatibility and manage dependencies.
- How Lerna Helps: Lerna automates versioning, ensuring that all packages in the monorepo have the same version number when a release is made.
- Dependency Management:
- Use Case: Managing dependencies across multiple packages becomes challenging, especially when some packages depend on others.
- How Lerna Helps: Lerna assists in managing dependencies by optimizing the installation of shared dependencies and avoiding duplication.
- Parallelized Tasks:
- Use Case: You want to speed up tasks such as running tests, building, or linting across multiple packages concurrently.
- How Lerna Helps: Lerna provides concurrency options, allowing you to parallelize tasks and significantly reduce the time needed for development tasks.
- Automated Releases:
- Use Case: You need a streamlined process for versioning and publishing packages whenever changes are made.
- How Lerna Helps: Lerna automates the release process, making it easier to bump versions, create Git tags, and publish updated packages.
- Simplified Task Execution:
- Use Case: Executing commands or scripts across all packages is a common requirement for tasks like linting or running tests.
- How Lerna Helps: Lerna simplifies the execution of tasks across packages with commands like
lerna exec
andlerna run
.
- CI/CD Integration:
- Use Case: You want to integrate the monorepo structure with your continuous integration/continuous deployment (CI/CD) pipeline.
- How Lerna Helps: Lerna is compatible with popular CI/CD systems, allowing you to seamlessly integrate it into your automated build and deployment processes.
- Efficient Workspace Configuration:
- Use Case: You aim to structure your monorepo efficiently, providing a clear organization of packages and their dependencies.
- How Lerna Helps: Lerna helps configure workspaces, allowing you to specify package locations and manage dependencies in a structured manner.
- Task Automation and Maintenance:
- Use Case: Automating routine tasks such as cleaning or repairing the monorepo is crucial for maintaining a healthy codebase.
- How Lerna Helps: Lerna offers commands like
lerna clean
andlerna repair
for automating maintenance tasks.
7. Conclusion:
In conclusion, Lerna is a powerful tool that streamlines the development workflow in monorepos. While it may have a learning curve, the benefits it brings to managing complex projects make it a valuable addition to the toolkit of any front-end engineer or team lead.
References:
- https://medium.com/@harshverma04111989/creating-monorepo-using-lerna-dd431d0db072
- https://www.startechup.com/blog/what-is-lerna
Visit Techtalkbook to find more related topics.