GitHub Actions — How to Test Lerna Monorepos

GitHub Actions provide a convenient way to test your project. Typically, you’ll trigger test runs and builds through push or pull request events.

When testing lerna monorepos, there’s a nuance that you must keep in mind: the CI environment! This tutorial shows you how to configure lerna to test your monorepo’s packages.

GitHub Actions Series Overview

Testing a Lerna Monorepo in GitHub Actions

Setting up your lerna project involves the lerna bootstrap step. Lerna installs all external dependencies and resolves the internal packages.

Lerna Detects CI Environments by Default

Lerna automatically detects CI environments and uses npm ci to bootstrap the packages instead of npm install.

With npm ci, you may run into bootstrapping issues like the one below where NPM can’t resolve a dependency because it isn’t published in the NPM registry yet:

$ lerna bootstrap

lerna notice cli v3.22.1  
lerna info versioning independent  
lerna info ci enabled  
lerna info Bootstrapping 11 packages  
lerna info Installing external dependencies  
lerna ERR! npm ci exited 1 in '@supercharge/container'  
lerna ERR! npm ci stderr:  
npm ERR! cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.

npm ERR! A complete log of this run can be found in:  
npm ERR!     /home/runner/.npm/_logs/2020-12-21T19_18_36_637Z-debug.log  

Solution: Use Lerna With the “--no-ci” Flag

Properly bootstrap your packages with lerna using the --no-ci flag. This explicitly instructs lerna to use the default behavior instead of switching a CI mode:

lerna bootstrap --no-ci  

That’s the trick!

Enjoy testing your lerna projects with GitHub Actions!

Mentioned Resources

  • lerna repository on GitHub

Explore the Library

Find interesting tutorials and solutions for your problems.