nakam blogs


nakam blogs


NPM Packaging - Publishing to Github

nakamume's photo
·Apr 29, 2021·

2 min read

Github Packages gives you registries for commonly used packages, such as Node(npm), Docker, RubyGems, Apache Maven, Gradle

In this post, we'll explore how to publish node packages using npm to Github and how to consume npm packages from Github.

Let's start with writing a very simple node package

  1. Create a github repo my-node-package (or whatever name you prefer 😉)
  2. Clone the repo and add your main JS file there
    git clone<SCOPE>/my-node-package.git && cd my-node-package
    touch index.js
  3. [Important] <SCOPE> should be replaced by your github username or organization name.
  4. Add code for your package. In my case, my package will simply return You have succesfull installed and ran lionbridgeai's rei package. So, the index.js file would look like
    module.exports = function rei() {
    return "You have succesfull installed and ran lionbridgeai's rei package";
  5. Let's add a package.json file. This is the only required file for a package.
     "name": "@<SCOPE>/my-node-package",
     "version": "1.0.0",
     "description": "This is an example node package",
     "repository": "<SCOPE>/my-node-package",
     "main": "index.js",
     "publishConfig": {
        "registry": ""
    Here name is scoped package name. version is package's version in semantic notation. repository is the github repo you created in Step 1, if your repo is private your package would be private otherwise your package will be public. main is the entrypoint for the package, in our case it's index.js file and finally publishConfig would tell the npm about the target registry for publishing.
  6. That's it. We have written our example package. In next section, we'll see how to publish it.

Publishing the package

  1. Create a PAT(personal access token) from Github with repo and write:packages permissions
  2. Do npm login using your github username as username and personal access token as password.
    npm login --scope=@<SCOPE> --registry=
  3. voilà! We are ready to publish
    # update your package's major, minor or patch version
    npm version major
    # publish it
    npm publish

Installing package from Github

  1. As we did in last section, login to github packages(required if installing a private package). And append the following config for github registry in your npmrc file
    Remember the scope? It's github org or user id.
  2. That's it! Run npm install <package-name> to install

If you are interested, join me in part 2, where we'll explore how to add unit tests for our package and to make use of github actions to automatically run tests and publish

Share this