DEV Community

Cover image for Simple guide to create package in Github registry
sifatul
sifatul

Posted on

Simple guide to create package in Github registry

Creating a package in the GitHub registry is very simple.

Firstly package.json needs to be updated; so that name, repository and publishConfig does not mismatch.

  "name": "@<owner>/<repo-name>",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/@<owner>"
  },
  "repository":"https://github.com/<owner>/<repo-name>.git",

Enter fullscreen mode Exit fullscreen mode

Secondly, create a GITHUB_TOKEN which would be used in verification.

The package can be published by:
a) GitHub action or
b) manually in terminal

a) To publish with GitHub action
add a workflow in .github/workflows/<workflow-name>.yml

name: Node.js Package

on:
  push:
    branches: ["<branch-name>"]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 12
      - run: npm ci
      - run: npm test

  publish-gpr:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
      - run: npm ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}


Enter fullscreen mode Exit fullscreen mode

b) publish package manually in terminal

Firstly, create a .npmrc file at the root of the directory

@<owner>:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=<GITHUB_TOKEN>
Enter fullscreen mode Exit fullscreen mode

Secondly, login to npm
npm login --scope=@<owner> --registry=https://npm.pkg.github.com

Finally, publish the package.
npm publish
sample project in GitHub

How to use a private package in another repository

create a .npmrc file at the root of the directory with github token having access to the private package

@<owner>:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=<GITHUB_TOKEN>
Enter fullscreen mode Exit fullscreen mode

npm install will install the package normally

PS: for public package in github repository no additional task is required

Useful links:

  1. quickstart
  2. working-with-the-npm-registry

Or
if you want to publish package in npm registry

Top comments (0)