Managing Multi‑Package Projects with Lerna: Concepts, Configuration, and Commands
This article explains the challenges of collaborating on multiple tightly coupled JavaScript packages, compares multirepo, monorepo and submodule strategies, introduces Lerna as a monorepo tool, and provides detailed guidance on its configuration, modes, and command‑line usage for efficient package management.
The article, originally from the Qiwang Weekly by Liu Guanyu, a senior front‑end engineer at 360, introduces the difficulties of collaborating on multiple interdependent software packages, such as complex dependency handling, unavailable in‑development packages, version‑management overhead, and circular‑dependency risks.
It then reviews three common collaboration models: multirepo (separate repositories for each package), submodules (Git‑based references), and monorepo (a single repository containing all related packages). The author notes that monorepo has become the preferred approach for many front‑end projects, citing Babel, vue‑cli, and create‑react‑app as examples.
Lerna, a tool created to manage large numbers of JavaScript packages, is presented as the de‑facto solution for monorepo management. The article describes Lerna’s two operating modes: fixed (all packages share a single version defined in lerna.json ) and independent (each package can have its own version). The --independent flag can be added during lerna init or set directly in the configuration file.
A typical lerna.json configuration is shown: { "version": "1.1.3", "npmClient": "npm", "command": { "publish": { "ignoreChanges": ["ignored-file", "*.md"], "message": "chore(release): publish" }, "bootstrap": { "ignore": "component-*", "npmClientArgs": ["--no-package-lock"] } }, "packages": ["packages/*"] } The configuration fields are explained: version (global version or independent ), npmClient (npm, yarn, cnpm, etc.), command (custom options for publish and bootstrap ), and packages (location of sub‑packages). Key Lerna commands are listed with examples: lerna init – creates a new monorepo (add --independent for independent mode). lerna create <package> [-y] – creates a new package inside packages . lerna add <package>[@version] [--dev] [--exact] [--scope=<module>] – adds a dependency to one or all packages. lerna exec [options] -- <command> – runs an arbitrary command in every package. lerna run <script> – runs an npm script defined in each package’s package.json . lerna bootstrap – installs all dependencies, links packages, and runs prepublish / prepare scripts. lerna publish – publishes all packages. lerna clean – removes all node_modules directories. Optimization tips include using lerna bootstrap --hoist to install shared dependencies once at the root, adding a publishConfig with "access": "public" for scoped public packages, and leveraging Lerna’s built‑in circular‑dependency detection during bootstrap. Finally, the article provides reference links to Jamie Kyle’s GitHub and the official Lerna website.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.