Creating and publishing your first GitHub private package using Lerna

shravan kashyap
3 min readApr 9, 2021

--

source: github.com

Github introduced their Github package registry since May 2019, a package Management service just like NPM packages. This allows us to manage packages right next to our source code!!.

Step 1: Setup the mono-repository

I will use my monorepo-test private git repository, and private packages within, as a working example.

Let’s get started!!

In the terminal I create a new project directory and initialise git and npm:

Initialise Lerna with independent versioning enabled:

Step 2: Populate the packages directory with subdirectory for each package

I will create two packages inside packages directory with names pkg1 and pkg2 and run npm init within each of these to create respective package.json. In order to publish your package to Github Package Registry you need to make minor adjustments to all package.json files present in each of the packages:

  • Add publishConfig entry in package.json, otherwise it will publish the package to npm registry!.
  • In order to publish multiple packages to the same repository you need to repository field.

The final package.json should look something similar to this:

This will end up creating a package name @kskashyap94/pkg1. You need to replace with your username or organisation name when working with your own packages.

Step 3: Creating a Personal Access Token

Login to your Github account > Settings > Developer Settings > Personal access tokens

Step 4: login to npm.pkg.github.com

kukashyap@MacBook-Pro.local [~]$ npm login — registry=https://npm.pkg.github.com

Username: <github-username>
Password: <your-personal-access-token> (generated in step 3)
Email: (this IS public) <your-email address>

Logged in as kulkarni-kashyap on https://npm.pkg.github.com/.

Step 5: Publish the packages

Create a private git repository and push the changes made with your initial commit containing the packages.

To publish the packages to github package registry, we make use of lerna publish command from the root directory of project.

This will prompt us to choose the version for this release.

Alternatively we can explicitly pass major/minor/patch along with publish

Once you receive Lerna Successfully published Package response, you can view your packages on github repository.

Step 6: Installing the private package

To install a private github package, you first need to create a .npmrc file at the root of consuming project if one doesn’t exist already and add the following entries:

//npm.pkg.github.com/:_authToken=TOKEN
@your-username:registry=https://npm.pkg.github.com

Replace TOKEN with personal access token and @your-username with your username or organisation name.

TOKEN is used for authentication to github packages whereas the second line ensures that packages scoped to your username/organisation name are downloaded from github package registry.

Step 7: Consuming the installed package

// index.js
const package1 = require(‘@kskashyap94/pkg1’);

I encountered a weird behaviour when working on this where lerna didn’t throw meaningful error message and here is the what I did to overcome and publish to registry successfully.

  • Ensure that the scope name mentioned in publishConfig field and the owner of monorepo (container repo) mentioned in repository field should match. Lerna publish throws 401 error if both doesn’t match with no error message :(

References:

https://docs.github.com/en/packages/guides/configuring-npm-for-use-with-github-packages

--

--

No responses yet