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?
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 –
- Initializing Lerna:
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-b, within the
- Linking Packages:
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:
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
lerna run: Run a script in each package.
lerna run test
lerna add: Add a dependency to packages.
lerna add some-package
4. Pros and Cons of Lerna:
|Lerna simplifies the management of large-scale projects by optimizing workflows and providing tools for versioning and publishing.
|Easy linking and testing between packages make cross-package development a breeze.
|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.
|Transitioning to Lerna may require some learning, especially for teams new to monorepo structures.
|In smaller projects, Lerna may introduce unnecessary complexity and overhead.
|Setting up Lerna and configuring the monorepo structure can take time initially.
5. List of commands:
|Interactive prompt to generate task runner configuration.
|List local packages that have changed since the last tagged release. [aliases: updated]
node_modules directory from all packages.
lerna clean -y
node_modules directory without asking for confirmation.
|Creates a new package within the
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.
|Displays information about the Lerna version, configuration, and packages.
|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.
|List local packages [aliases: ls, la, ll].
lerna publish [bump]
|Publish packages in the current project.
|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.
|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.
|Installs all dependencies and links any cross-dependencies.
(Deprecated) The “bootstrap” command was removed by default in v7, and is no longer maintained.
|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
packages directory to use symlinks.
|What level of logs to report. [string] [default: info]
|How many processes to use when lerna parallelizes tasks. [number] [default: 10]
|Fail if a cycle is detected among dependencies. [boolean]
|Disable progress bars. (Always off in CI) [boolean]
|Do not sort packages topologically (dependencies before dependents). [boolean]
|Set max-buffer (in bytes) for subcommand execution [number]
|Show help [boolean]
|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.
- 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
- 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 repairfor automating maintenance tasks.
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.
Visit Techtalkbook to find more related topics.