Deploy NPM package to Github package registry

This article will demonstrate how to publish a package to Github package registry.

Files that need to prepare

  1. package.json
  2. .npmrc
  3. Github Personal access token with read, write, delete packages permission

Step 1: Prepare package.json

On your local project package.json on your root project.

{
"name": "@<Github or Org Name>/<Repo name>",
  "description": "xxx",
  "license": "MIT",
  "author": "ookangzheng",
  "version": "1.0.1",
  "main": "build/index.js",
  "bin": {
    "<CLI Name>": "./build/index.js"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/<Github username>"
  },
  "repository": {
    "url": "git@github.com:<Github username>/<Repo name>"
  },
}

Inside build folder which contains all files that ready to deploy.

Edit package.json that located inside build folder. dependencies data will include some node module that you want to install on client machine.

{
"name": "@<Github or Org Name>/<Repo name>",
  "description": "xxx",
  "license": "MIT",
  "author": "ookangzheng",
  "version": "1.0.1",
  "main": "build/index.js",
  "bin": {
    "<CLI Name>": "./build/index.js"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/<Github username>"
  },
  "repository": {
    "url": "git@github.com:<Github username>/<Repo name>"
  },
  "dependencies": {
    "ejs": "^3.1.2",
    "fs-extra": "^9.0.0",
    "inquirer": "^7.1.0"
  },
}

Step 2: Prepare .npmrc

First, you have to generate Personal access token with read:package, write:package, delete:package permission.

File .npmrc on your project root directory.

strict-ssl=false
registry=https://npm.pkg.github.com/<Github Username>
//npm.pkg.github.com/:_authToken=< Personal access token >

Npm under proxy

// .npmrc
npm config set https-proxy http://127.0.0.1:3128
npm config set proxy http://127.0.0.1:3128

References:
1. https://dev.to/sijils/publishing-npm-package-to-github-package-repository-5ane
2. https://zellwk.com/blog/publish-to-npm/
3. https://www.clcoder.com/2019/12/31/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8GitHub%20Packages%E5%88%9B%E5%BB%BAnpm%E7%A7%81%E6%9C%89%E5%BA%93/