Manage a monorepo with Lerna
February 25, 2021
Lerna is a flexible tool for managing monorepos. It provides tools for installing, building, versioning and publishing multiple packages.
Even though Lerna is targed at monorepos, it can be used to manage a single package.
Configuring Lerna
lerna.json{” “}
- Use
fixedversioning so all packages move in lockstep- Consumers simply need to install the same version
- Ensure to run
lerna versionwith--force-publishso that all packages are upgraded regardless whether they have changed independentversioning can be used when the packages in a monorepo do not import one another, i.e. are truly independent
- Set
npmClienttoyarnto use the Yarn Workspaces backend which is noticeably faster when installing
- Use
package.json{” “}
- Include the
workspaceblock to use Yarn Workspaces
- Include the
Installing
Installing
yarn install- Yarn Workspaces will manage installing and deduping external packages
- There should be a single
node_modulesandyarn.lockat the root of the monorepo
Install additional packages in the root
yarn add rimraf --dev -W-Windicates to install at the rootpackage.json
Install additional packages
lerna add rimraf --scope package-1package-1is the name of a package in the monorepo- The same command can be used to install one monorepo package in another monorepo package
Other scripts
Run scripts
lerna run build- This will run the
buildscript in any package that defines it in thepackage.json - The order that this script is run is determined by the package dependency on one another
- e.g. if
package-2installedpackage-1, there is a dependency, thus scripts must first be run inpackage-1beforepackage-2
- e.g. if
- This will run the
Publish workflow
Versioning and publishing are performed separately.
lerna versionis run locally to bump all package versions, produce a changelog then tag and commit these changes.lerna version --force-publish --conventional-commits --conventional-prerelease -m \"chore: version\"--conventional-commitswill generate aCHANGELOG.mdsince the last version--conventional-prereleasewill create a prerelease version, e.g.0.1.0.alpha.1--conventional-graduatewill create a stable release version, e.g.0.1.0
- A new commit with message defined by
-mwill be pushed
lerna publishis run on CI to publish versions that do not exist in the package repository (default NPM)lerna publish from-package --loglevel=verbose --no-verify-access --dist-tag prerelease --yes--dist-tagdetermines the release channellatestis the default channel used when someone install without specifying a versionnpm install package-1- Only publish to this channel when making an official stable release, i.e. after a
lerna version --conventional-graduate
- Only publish to this channel when making an official stable release, i.e. after a
prereleasewill only be installed if the version is specified explicitly or@prereleaseis the versionnpm install package-1@prerelease- Useful for testing our own releases
- Most understand that versions released can be subject to breaking API changes
--yesmake this CI friendly by skipping interactive confirmation