Publishing private npm packages using GitHub Packages
Tags: Programming, NPM, Github, Software Development, JavaScript

Publishing private npm packages using GitHub Packages Victor Follow Jun 14 · 4 min read

Photo by Pixabay from Pexels

In this article, I’ll be going over how to use GitHub Packages to publish your npm packages. If you are not familiar with GitHub Packages, it lets you publish to their registry npm, gem, or NuGet packages just to mention a few.

First, you’ll need an access token…

You’ll need to generate what is known as a personal access token. This token will allow you to be able to authenticate with GitHub’s API and publish your package. To create one, you will have to go to Settings / Developer Settings / Personal Access Token. Here you will have to click on Generate new token.

In the new personal access token screen, you’ll have to select the scopes of repo, write:packages, and read:packages. Make sure to add a note so you know what this token is for and click on the Generate token button at the bottom.

Important: Make sure to save this token in a secure place, because you’ll need this token later. Once you’ve successfully generated your token, you will need to create a secret in your repository. For this, go to your repository on GitHub, then to Settings / Secrets, and click on New Secret.

For the name, enter NPM_AUTH_TOKEN and on the value field and paste the value of the token you’ve generated.

Updating your package.json

You will now need to make a few changes to your package.json on your repository so that your library can be published. First, you’ll have to change the name of your package to have as the scope your username or your organization’s name, followed by the package’s name. This will be in the name property inside your package.json. For example, if your username is your_username and your project name is your_project_name you will end up changing the package’s name to be the following.

"name": "@your_username/your_project_name"

Next, you’ll have to specify which is the registry you will be publishing your package to. You will have to add this entry and replace your_username with either your username or organization’s name.

"publishConfig": {

"registry": "https://npm.pkg.github.com/your_username"

}

Finally, if your project is inside a monorepo, you will have to add this entry so you are allowed to publish multiple packages within the same repository.

"repository": {

"type": "git",

"url": "ssh://git@github.com:your_username/your_project_name.git",

"directory": "@your_username/your_project_name"

}

Package access

As of now, by default GitHub Packages inherits the access from your repository. Given this, as long as your repository is private, your package will also be private. You are going to have issues if you keep the property of private:true inside your package.json. You can remove this property as it won’t let you publish your package with it.

And now… .npmrc

The next step will be for you to create a file named .npmrc. This file should be created at the root of your project and should contain the following contents.

registry=https://npm.pkg.github.com/@your_username

registry=https://registry.npmjs.org

Publishing your package from your workstation First, you’ll need to login to the GitHub Packages registry on your workstation. To do this, you can run the following npm command.

npm login —registry=https://npm.pkg.github.com —scope=@your_username

Once you run this command, it will ask you for your username, password, and e-mail. Make sure to use the access token you’ve previously generated as the password. After you’ve authenticated, go to the dist folder of your project and run npm publish.

cd dist

npm publish

Installing your package

To consume your package, you will have to add the same .npmrc file you added to your publishing library project to your consuming project. In addition to this, you will need to run an npm install command like the following.

npm install @your_username/your_project_name@version

And at this point, you should be all good to go! Enjoy!

Something to keep in mind

Nothing in life is free. While the prices for GitHub Packages are pretty generous, always make sure to keep an eye on that and set up a spending limit if necessary.

JavaScript In Plain English

Enjoyed this article? If so, get more similar content by subscribing to our YouTube channel!