Getting Started with the GitHub Package Registry
This post originally appeared at https://www.thewebdevcoach.com/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:
- 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):
- On the left sidebar, click Developer settings. This should take you to https://github.com/settings/apps
- On the left sidebar, click Personal access tokens. This should take you to https://github.com/settings/tokens.
-
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
-
Scroll down to the bottom and click the green Generate token button.
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.
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
- Add a
.npmrc
file to the root of your project:touch .npmrc
. - Inside your
.npmrc
file, write the following:
# .npmrc
registry=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.
- Inside your
package.json
file, please modify or add the following:
// package.json
{
// ...things you have already
"name": "@your-github-name/package-name",
"repository": "git://github.com/your-github-name/package-name.git"
// ... other things you have already
}
- Publish:
npm publish
.
Using publishConfig
in package.json
- Inside your
package.json
file, please modify or add the following:
// package.json
{
// ...things you have already
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"name": "@your-github-name/package-name",
"repository": "git://github.com/your-github-name/package-name.git",
// ... other things you have already
}
- Publish:
npm publish
.
Installing from the GitHub Package Registry
- Authenticate
- Add
.npmrc
to have:
# .npmrc
@the-github-name-of-the-org-or-person-who-created-the-package-in-lowercase:registry=https://npm.pkg.github.com
- Now you can successfully run
yarn
ornpm 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.
Top comments (0)