Publish NPM packages with Github Actions

Rishikesh Dhokare
4 min readAug 10, 2020
from GitHub

About a couple of years back, I created a NPM library to create test data. Nothing fancy, just a bunch of javascript functions creating various types of test data like strings, dates, numbers etc. The purpose was not to create an open source library but more to learn the end to end process of building something and making it public by publishing to NPM registry.

The process involved few important things like automating the test coverage calculation using coveralls, building and testing the library using travis CI and finally publishing the library to npm registry.

With GitHub introducing the actions/workflows recently, It brought all things together and I thought that I should be automating this process. This blog post is an attempt to describe how I used GitHub workflows to automate the building and publishing process for a NPM library.

1. Create a workflow for building & testing

It is as simple as going to the “Actions” tab on your GitHub repository and choosing a workflow template that suites your need. In my case, since it was a Node.js project, I choose respective workflow template. It looked like below —

choosing a workflow template

Once you click on the “Set up this workflow” button, it takes you to an editor where a yaml file with a selected set of steps is pre-configured for you.

You can rename the file to call it specific to what you want to do. I renamed it to test.js.yml since I wanted to only install dependencies and run tests as part of this workflow. As soon as you save the file with a commit, it gets saved in .github/workflows folder in your project files.

The file contains —

  1. triggers on which the workflow should start e.g. when you push code into master branch or pull requests are created etc.
  2. Set of jobs to run as part of this workflow e.g. test

Additionally you can configure your workflows to run on various versions of Node.js.

Here is the file from my project for reference.

2. Configure NPM token

Since you want to publish the library to NPM, you need to have a token from NPM which you need to configure as secret in your GitHub repository. To do that,

  1. Go to your npmjs account →
  2. Go to Auth Tokens
  3. Click on Create New Token
  4. Select the Access Level as Read and Publish
  5. To configure it in your GitHub repository, go to SettingsSecrets and add a New secret and paste the token created in above steps
select access level for the token
create a npm token
configure the NPM token as secret in your Github repository

3. Create a workflow for publishing to NPM registry

Once the test workflow passes and you have a NPM token configured, next step is to publish the library to NPM registry. Following similar step #1, go to Actions tab and select the template Publish Node.js Package which will take you to the corresponding yaml file. Here you need to provide reference to the registry-url which is https://registry.npmjs.org and then add the NPM token as an environment variable so that the workflow will pick it’s value from the configured secret while communicating with NPM registry.

publish to NPM workflow

Based on the triggers you can see how the workflows look in your “Actions” tab. For example this is how the workflows look for my project —

Github workflows

References:

  1. GitHub — https://github.com/rishikeshdhokare/fakedata
  2. NPM — https://www.npmjs.com/package/@rishikeshdhokare/fakedata

Hope this helps you! Don’t forget to share and clap! 🙂

--

--

Rishikesh Dhokare

I am a Software Engineer from India and working in Berlin, Germany. I write about technology, my experiences in Germany, travel in Europe.