LinuxCommandLibrary

lerna

Manage JavaScript projects with multiple packages

TLDR

Initialize project files (lerna.json, package.json, .git, etc.)

$ lerna init
copy

Install all external dependencies of each package and symlink together local dependencies
$ lerna bootstrap
copy

Run a specific script for every package that contains it in its package.json
$ lerna run [script]
copy

Execute an arbitrary shell command in every package
$ lerna exec -- [ls]
copy

Publish all packages that have changed since the last release
$ lerna publish
copy

SYNOPSIS

lerna <command> [options]
For example:
lerna bootstrap
lerna publish
lerna run <script-name> [...args]

PARAMETERS

--help
    Displays help information for lerna or a specific subcommand.

--version
    Shows the lerna CLI version.

--loglevel <level>
    Sets the logging level (e.g., silent, error, warn, info, verbose, silly).

--yes
    Skips all confirmation prompts, assuming 'yes' to all.

--scope <package-name>
    Restricts commands to a subset of packages that match a glob. Can be specified multiple times.

--ignore <package-name>
    Excludes packages that match a glob from being processed. Can be specified multiple times.

--hoist
    (For bootstrap) Installs all dependencies to the root node_modules, rather than in each package's node_modules.

--stream
    (For run, exec) Streams output from concurrently running processes directly to the console.

--canary
    (For publish) Publishes packages with a canary version (e.g., 1.0.0-alpha.0+).

--conventional-commits
    (For publish, version) Uses Conventional Commits specification to determine new versions and generate changelogs.

--registry <url>
    (For publish) Specifies the npm registry to publish to.

DESCRIPTION

lerna is a powerful command-line tool designed to manage multi-package repositories, often referred to as monorepos, primarily for JavaScript and TypeScript projects. A monorepo is a single Git repository containing multiple distinct projects or packages. lerna streamlines the development and deployment process by providing utilities to manage dependencies, versioning, and publishing of these inter-dependent packages. It abstracts away much of the manual work involved in linking packages locally, running scripts across multiple packages, and maintaining consistent versioning schemes, especially when publishing to npm. While modern package managers like npm and Yarn now offer native workspace features, lerna continues to provide advanced capabilities, particularly around publishing automation, independent package versioning, and executing scripts across a filtered set of packages, making it a valuable tool for complex monorepo setups.

CAVEATS

lerna is specifically designed for JavaScript/TypeScript monorepos and might not be suitable for polyglot monorepos. While it offers powerful automation for publishing and versioning, its complexity can be an overhead for very simple monorepos or when native package manager workspaces (like npm workspaces or Yarn workspaces) suffice. The project has seen periods of varying maintenance activity, which is a consideration for long-term project stability, though it has recently seen renewed development.

MONOREPO CONCEPT

A monorepo is a software development strategy where code for many projects is stored in the same repository. Unlike a traditional multi-repo setup, a monorepo centralizes code, simplifying dependency management, code sharing, and atomic changes across related components. lerna provides the scaffolding and tooling to effectively manage the inter-dependencies, build processes, and publishing workflows within such a consolidated repository.

CONFIGURATION FILE (LERNA.JSON)

lerna's behavior is primarily configured via a lerna.json file located at the root of the monorepo. This file specifies important settings such as the versioning strategy (fixed/independent), the paths to packages within the repository, and other global configurations. It's crucial for defining how lerna identifies and interacts with the various packages.

HISTORY

lerna was created by Sebastian McKenzie (known for Babel) and first released in 2016. Its primary motivation was to simplify the management of large JavaScript projects composed of multiple packages, a pattern popularized by projects like Babel itself. Before npm and Yarn introduced native workspace support (in 2017 and 2018 respectively), lerna was a groundbreaking tool for tackling monorepo challenges like shared dependencies and consistent versioning. Although the landscape has evolved, lerna's influence on how monorepos are managed in the JavaScript ecosystem is significant, and it continues to be a viable solution for many teams, especially those requiring advanced publishing workflows.

SEE ALSO

npm(1), yarn(1), git(1), nx(1), turborepo(1)

Copied to clipboard