GitHub Package Registry 简单试用记录

3 min read

简单记录下 GitHub Package Registry 怎么用

Example Repo: gh-pkg-demo

How to use GitHub Package Registry

1. Generate Personal Access Token

Generate the token according to your need.

2. Local config TOKEN

Add the generated token to the local file ~/.npmrc like:


Create the file ~/.npmrc if it doesn’t exist. This is the global user config file for npm.

If you don’t want to set TOKEN locally, then one should log in by npm login command and input the info of your GitHub account with prompt like:

$ npm login --registry=
> Username: USERNAME
> Password: TOKEN

3. Project Config to publish to GitHub

Edit the package.json file to specify the registry as GitHub. And also the package name must be scoped with the onwer, @OWNER/PKG-NAME.

--- "name": "gh-pkg-demo",
+++ "name": "@bambooom/gh-pkg-demo",
+++ "publishConfig": {
+++   "registry": ""
+++ },

Alternative way to config the project is using local .npmrc file. That is adding a local .npmrc under project root, and adding this to the file:


4. Publish packages

To publish the package to GitHub, just use the command:

$ npm publish

You can see the package on GitHub:

5. Install packages

To install packages from GitHub package registry, need a local .npmrc file to set the registry.


But if only using this setting, all package requests will go through GitHub Packages.

You also need to add the .npmrc file to your project so all requests to install packages will go through GitHub Packages. When you route all package requests through GitHub Packages, you can use both scoped and unscoped packages from

So, the github package requests will do the check whether the package can be found on github. If not, it will go to to search for the packages.

However, I found that the check request will time out randomly (may be due to GFW). I’d like to skip the check for those packages I want to download from

I try to change the setting in .npmrc like this to achive this goal:


With this setting, default registry is Only @OWNER’s packages use the GitHub registry.

Then use the command to install:

npm install @bambooom/gh-pkg-demo

Look inside package-lock.json, the package is resolved by GitHub registry like:

"@bambooom/gh-pkg-demo": {
  "version": "1.0.0",
  "resolved": "",
  "integrity": "...."