Publish Library as an NPM & Github module with CJS & ESM support 🌟

Publish Library as an NPM & Github module with CJS & ESM support

nestjs( "module")

tkssharma/blogs Creating your first package This section is for you if you haven't published a package to npm before. Feel free to skip…

Part-1 (Publish as NPM Package )

Creating your first package This section is for you if you haven’t published a package to npm before. Feel free to skip to the next section if you’ve published one before.

To publish your first package to npm, you need to go through these steps:

  • First, you need to have an npm account. Create one here if you don’t have one yet.
  • Second, you need to login to your npm account through the command line. (You need to have Node and npm installed on your system before you perform this step. Install them here). You can now do npm Login and pusblish
  • npm login
  • npm publish — access public
    ➜  publish-nodejs-typescript-modules git:(master)npm publish --access public
    npm notice 
    npm notice 📦  @tkssharma/test-package@1.0.1
    npm notice === Tarball Contents === 
    npm notice 2.5kB package.json        
    npm notice 37B           
    npm notice 88B   lib/cjs/index.d.ts  
    npm notice 196B  lib/cjs/index.js    
    npm notice 205B  lib/cjs/
    npm notice 88B   lib/esm/index.d.ts  
    npm notice 108B  lib/esm/index.js    
    npm notice 205B  lib/esm/
    npm notice === Tarball Details === 
    npm notice name:          @tkssharma/test-package                 
    npm notice version:       1.0.1                                   
    npm notice package size:  1.6 kB                                  
    npm notice unpacked size: 3.4 kB                                  
    npm notice shasum:        92b5c278e35a7a573d00cdedc6635f5b01842f33
    npm notice integrity:     sha512-9HvXq7+ZH94xV[...]lDVxsCxiSBjBA==
    npm notice total files:   8                                       
    npm notice 
    + @tkssharma/test-package@1.0.1

Part-2 (Publish as Github Package )

Github package repository is where you can publish npm, gem, mvn, nuget, gradle, docker packages and is currently now in beta. In this tutorial, we will see how we can publish an npm package to the github package respository.

  • Code the node.js project you want to publish as npm module in github repo.
  • Create a github repository in which you will be publishing the package

Lets make your package.json ready

Amend the repository to the ssh URL of the repo (e.g. You can find this by using the ‘Clone or download’ button using the ‘Clone with SSH’ option.

Update the name of the package. This must match the repo URL but with @tksssharma rather than just tkssharma (e.g. becomes @tkssharma/package-name).

Add a description for the package.

You must not change or remove the URL in publishConfig. That ensures that the package is published on GitHub rather than

"name": "@tkssharma/package-name",
  "publishConfig": {
    "registry": ""
  "repository": {
    "url": ""

Lets check on package.json

  "dependencies": {},
  "description": "npm module for node js development",
  "devDependencies": {
    "reflect-metadata": "^0.1.13",
    "@types/node": "^13.9.0",
    "typescript": "^3.8.3"
  "keywords": [
  "main": "lib/index.js",
  "name": "@tkssharma/package-name",
  "publishConfig": {
    "registry": ""
  "repository": {
    "url": ""
  "scripts": {
    "build": "tsc",
    "build:watch": "tsc -w",
    "prepublishOnly": "tsc"
  "types": "lib/index.d.ts",
  "version": "1.0.0"

Login to the github npm registry from the console using the below command

npm login --registry= --scope=@tkssharma

@tkssharma is the username of your github account. Executing this query will ask for username and password. Username is your github username. Password is Personal Access Token which can be generated from your github account settings page.

Run npm publish command from the project directory. This will publish your node.js project as npm module to github repository. The github repository link is @tkssharma/packages . The module will be published in the scoped mode.

Now the module can be installed by anyone from this repository. To do this, the user who intends to install the module should set the npm registry in .npmrc file as registry= . Once this configuration is done, the npm module can be installed by running the command npm install @tkssharma/npmmodulename

  • host npm source code in GitHub
  • host npm package (both publicly and privately) in Github Packages
  • build, version and publish a npm package via GitHub Actions
  • Install GitHub hosted npm package in your project

We will use yarn as the package management tool (in most of the cases). You can replace it with npm and get the same result

Host NPM package in Github Packages

The NPM package hosted by GitHub is scoped with your GitHub account name: @github-username/package-name. This can be set by the name attribute in package.json.

It supports both public and private package (based on your GitHub repo is public or private).

The NPM publishing destination (rather than the default is controlled by publishConfig setting in package.json:

"publishConfig": {
        "registry": ""

If you want to publish from your local machine, please follow the steps:

  1. You need a Personal access token from GitHub to be used as password on command line. It can be generated from Settings > Developer settings > Personal access tokens with correct permissions (at least read:packages, write:packages and repo, delete:packages optional).
  2. Run npm login --registry= and follow the prompt. (Note that yarn login doesn’t support specifying registry :( )
  3. Run yarn publish

(You only need to do steps 1 and 2 once and the authentication details will be stored in your ~/.npmrc).



  1. You cannot delete a public package yourself (similarly, npmjs only supports to delete a public package within 72 hours).

Build, Version and Publish NPM package via GitHub Actions

Place this publish.yml under .github/workflows/publish.yml:

# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: [](

name: Node.js Package deploy
    branches: [ master ]
    branches: [ master ]

    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
          node-version: 12
      - run: npm ci
      - run: npm test
    needs: build
    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
          node-version: 12
          registry-url: [](
          scope: "@tkssharma"
      - run: yarn install
      - run: git config --global "$" && git config --global "$GITHUB_ACTOR"
      - run: yarn version --minor
      - run: git push --tags && git push
      - run: yarn publish
          NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}


  1. Remember to replace scope with your own GitHub account name
  2. It triggers on new commits pushed onto master branch (the automatic push in the publish-gpr job won’t trigger!)
  3. Two jobs: build will run tests, if that passes, publish-gpr job will be triggered
  4. publish-gpr job will firstly bump the minor version number (e.g. 1.8.0 -> 1.9.0), tag git repo with the new version v1.9.0 and push back the new commit to GitHub (this has been done by yarn version). Then finally build and publish the new npm package to Github Packages (via yarn publish).
  5. Note that the authentication is done via the default GITHUB_TOKEN from the GitHub Actions environment.

Install GitHub hosted NPM package in your project

A project level .npmrc is required, so package manager knows where to download the package:


If your NPM package is hosted privately, you also need to login first via npm login — registry= See previous section for details.

Now yarn add @tkssharma/package-name will install my latest version of package-name to your project.

nestjs( "module")