Categories
javascript nodejs

Create a private NPM package in Github Packages

We will be deploying a private npm package to Github packages. This is like the public npm registry, but in our case it will be hosted on Github and be private.

Lets get started

Create a new repository in Github and make it private. We will then clone it down to our local machine. Mine is at https://github.com/aldomatic/private-npm-package but its private so you won’t be able to see it.

Navigate to the locally cloned repository and run the commands below.

npm init -y
touch index.js

mkdir .github
mkdir .github/workflows
touch .github/workflows/action.yml

We will need to update the package.json file. Below is the example for mine.

{
  "name": "@username/private-npm-package",
  "version": "1.0.0",
  "description": "Package for trying out GITHUB Package Registry",
  "main": "index.js",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/@username"
  },
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Notice the ‘name’ and ‘publishConfig’ properties have my @username. You will need to update that to your github username.

For the actions.yml file we can use the build script below.

name: Build and Deploy

on:
  push:
    branches:
      - main

jobs:
  publish-gpr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
          scope: '@username'
      - run: npm install
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.MY_GITHUB_TOKEN}}

Some Code

In the index.js file just add this simple function.

const sayHello = (name) => {
  console.log('Hello ' + name + '!');
};
  
module.exports = {
  sayHello,
};

Generate access token

In Github go to Settings > Developer Settings > Personal Access Token. Name your token and for the scopes we will select the below.

workflow
write:packages
delete:packages

Then generate the token and save it somewhere. In the previous steps above there is a secrets.MY_GITHUB_TOKEN. We will create a secret in our repository we created. Go to the repository page and select Settings > Secrets. Here add a new secret, name it how you want I left mine as MY_GITHUB_TOKEN and paste in the access token we created earlier.

Push the changes

At this point you should be able to push the local changes up to the repository. This will trigger an action and publish the package. If you go to actions tab you should see something like below. Below was my initial push and build.

Body of a man must work properly in both physical and mental way. canadian viagra for sale That’s why, a physician always recommends getting this oral drug when a man has sexual thoughts, these thoughts send signal through the spinal cord means disrupting the signals that come from the brain, levitra generika 5mg goes from circulatory system and then stops at reproductive health. Definitely, health related vardenafil price benefits of valerian are generally big, nonetheless too much a fantastic thing might make unpleasant side effects. An introduction of viagra cheap no prescription aids you to get rid of erectile Dysfunction safely.

If everything deployed correctly you should now see a new package under the packages section of your github profile.

Installing our private package

So, by this point we should have pushed code, built and it now appears in the package section of our profile. Now, lets install our new private package.

For this we will need to create a new project.

mkdir package-test
cd package-test
npm init -y
touch .npmrc
touch app.js

We need to edit our .npmrc file to point to the github package registry. Add the lines into the file.

@username:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${MY_GITHUB_TOKEN}

Make sure and update my username to yours and add your access token. Then run.

npm i @username/private-npm-package

If things went well you should now see your package in the node_modules folder.

In your app.js file try it out.

const { sayHello } = require('@username/private-npm-package');

sayHello('Oliver');