Navigate back to the homepage

Getting Started with the GitHub Package Registry

Aryan Jabbari
January 1st, 2020 · 2 min read

Getting Started with the GitHub Package Registry

What is GitHub Package Registry?

Like npm, the GitHub Package Registry hosts software packages both privately and publicly. These packages can be downloaded and used in your projects (like npm). Nothing much in your current workflow needs to change.

Authenticating to GitHub Packages

Create a Personal Access Token

If you haven’t already, you’ll need to create a GitHub personal access token for use in the command line:

  1. In the upper-right hand corner of any GitHub page, click your profile photo then click Settings (which should take you to https://github.com/settings/profile):
    GitHub settings dropdown
  2. On the left sidebar, click Developer settings. This should take you to https://github.com/settings/apps
  3. On the left sidebar, click Personal access tokens. This should take you to https://github.com/settings/tokens.
  4. Click the Generate new token button and create a new token with the following permissions:
    • read:packages: allows you to download packages from GitHub Package Registry
    • write:packages: allows you to upload packages to the GitHub Package Registry
    • delete:packages: allows you to delete packages from the GitHub Package Registry

Necessary permissions

  1. Scroll down to the bottom and click the green Generate token button.
  2. Click the copy to clipboard icon to copy the token to your clipboard. Make sure to store it in a safe place (perhaps LastPass or 1Password?) as this is the only time you’ll be seeing this token.
  3. You’ll use this token in the following steps to log into the GitHub Package Registry.

Please refer to the GitHub documentation for additional information.

Authenticate to GitHub Packages

Now it’s time to use the personal access token you just created. Run the following command:

  • npm login --registry=https://npm.pkg.github.com --scope=@your-github-name
    • This command will ask your for (GitHub) username, password (enter personal access token here) and your (public) email address. Use the token generated above as the password.
  • This information will then be saved at ~/.npmrc as //npm.pkg.github.com/:_authToken=TOKEN. Please verify that is true.

Publishing to GitHub Package Registry

You’re all ready to publish your first package in the GitHub Package Registry. Please note that your package will be published under @your-github-name/package-name (unlike what you’re likely used to with the npm registry.

Before trying to publish, make sure you’ve already authenticated.

There are two ways to publish to the GitHub Package Registry:

Using a Local .npmrc

  1. Add a .npmrc file to the root of your project: touch .npmrc.
  2. Inside your .npmrc file, write the following:
1# .npmrc
2registry=https://npm.pkg.github.com/your-github-name

Please note that your-github-name must be all lowercase! Additionally, this file must be committed to your GitHub repository.

  1. Inside your package.json file, please modify or add the following:
1// package.json
2{
3 // ...things you have already
4 "name": "@your-github-name/package-name",
5 "repository": "git://github.com/your-github-name/package-name.git"
6 // ... other things you have already
7}
  1. Publish: npm publish.

Using publishConfig in package.json

  1. Inside your package.json file, please modify or add the following:
1// package.json
2{
3 // ...things you have already
4 "publishConfig": {
5 "registry": "https://npm.pkg.github.com"
6 },
7 "name": "@your-github-name/package-name",
8 "repository": "git://github.com/your-github-name/package-name.git"
9 // ... other things you have already
10}
  1. Publish: npm publish.

Installing from the GitHub Package Registry

  1. Authenticate
  2. Add .npmrc to have:
1# .npmrc
2@the-github-name-of-the-org-or-person-who-created-the-package-in-lowercase:registry=https://npm.pkg.github.com
  1. Now you can successfully run yarn or npm i.

Note: This’ll also work when deploying to services like Netlify, Zeit Now, etc.

And that’s it! You can now install from and publish to the GitHub Package Registry. If you’ve any questions, take a look at the GitHub documentation for configuring npm for use with GitHub packages or feel free to reach out to me on Twitter.

Follow Me

📧 Join my email list and get notified about new content

Stay updated with all my blog posts and future randomness!

More articles from The WebDev Coach

Use Prisma with a NestJS GraphQL Server

Use Prisma with a NestJS GraphQL Server Before We Get Started In my last blog post , I described how to stand up a Node.js server with…

October 5th, 2019 · 4 min read

Stand Up a Node.js Server with NestJS including TypeScript and GraphQL

Stand Up a Node.js Server with NestJS including TypeScript and GraphQL Recently, I discovered NestJS and instantly fell in love. It is…

September 18th, 2019 · 3 min read
© 2018–2020 The WebDev Coach
Link to $https://twitter.com/aryanjabbariLink to $https://www.youtube.com/c/thewebdevcoachLink to $https://dev.to/aryanjnycLink to $https://github.com/AryanJ-NYCLink to $https://www.linkedin.com/in/aryanjabbariLink to $https://www.instagram.com/thewebdevcoach/