Douglas Moura

Douglas Moura

Software Engineer

Douglas Moura

Douglas Moura

Software Engineer, Musician and Jiujiteiro.

Use GitHub actions to publish your package on NPM

Published at:Published at:Updated at:

Use GitHub actions to publish your package on NPM

Recently, I created a package with the ESLint settings I like to use in my React projects, as I was tired of always having to configure it when I start new React projects. Publishing a NPM package is just a matter of running npm publish on the directory of your package (considering, of course, that you already have an NPM account and is authenticated on your terminal). But I wanted to automatize this publishing everytime I created a new release.

In order to do that, I used the following GitHub Action:

# File: .github/workflows/npm-publish.yml

# This workflow will publish a package to NPM when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Publish Package to npmjs

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org/
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}

If you read the YAML file above (that you should put on the .github/workflows/npm-publish.yml directory of your git repository), you should have noted that the environment variable NODE_AUTH_TOKEN should be defined. Create a new automation access token on the control panel of NPM:

  1. Access your NPM account and click in "Access tokens": Access tokens on NPM

  2. Name your new access token and select the "Automation" type for it:

Creating access token on NPM

  1. Go to your GitHub repository, click in "Settings > Secrets > Actions > New repository secret", name it as NODE_AUTH_TOKEN and paste the access token you just got from NPM:

Create a new secret on the GitHub repository

  1. Create a new release for your package. This should trigger our GitHub Action and publish to NPM.

Creating a new release on GitHub

Leave a Reply

Loading comments...