Home / Blog / Publish Your First NPM Package

Publish Your First NPM Package

6 min read.May 27, 2025

Publish Your First NPM Package

Lets dive in, how could we publish our own npm package, which can be installed by npm install command.

Npm stands for Node Package Manager , It is the default package manager of NodeJs projects. We often use npm to install, upgrade or manage various libraries in nodejs based projects such as a React project or a NextJs project etc.

But publishing your own package in npm not only help you to reuse your code again but also it might help the entire nodejs community to adopt your idea through the package.

Steps to create a Npm Package

  1. Choose a entry point of the package(bin directory will be better choice)

  2. Setting up package.json

  3. Generate a token to publish in npm

  4. Setup a CI/CD pipeline to auto publish the package in each push

Setting up entry point

Here is my directory structure and in my case the entry point will be bin/index.js

Directory structure
Directory structure

Setting Up package.json

set a unique name of the package this will be used while npm install command to install it. In my case it is byte-cliset the entry point in main propertyset the bin property inside that enter the command as key and value will be the entry point.setup publishConfig same as seen below. 

{
  "name": "byte-cli",
  "version": "2.4.0",
  "main": "./bin/index.js",
  "type": "module",
  "bin": {
    "byte-cli": "./bin/index.js"
  },
  "keywords": [],
  "author": "ArunEz",
  "license": "ISC",
  "description": "A CLI tool for Project Initiations",
  "dependencies": {
    "chalk": "^5.3.0",
    "commander": "^12.1.0",
    "inquirer": "^10.1.8"
  },
  "publishConfig": {
    "registry": "https://registry.npmjs.org/",
    "access": "public",
    "tag": "latest"
  }
}

Now we are all set at code level lets go to the publish part

Generate a npm token

Go to npm account section and click on generate token and generate a Classic Token, must select Publish,  copy the token and save it somewhere.

Npm classic token generated
Npm classic token generated

Setting Auto Publish

Create a publish.yml file inside .github/workflows Inside publish.yml file add the below workflow code.

  • Adjust the github branch name, in my case it is main branch.

  • In the code must replace YOUR_NPM_TOKEN with actual npm token you generated.

name: Publish to npm

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '22'
          registry-url: 'https://registry.npmjs.org/'

      - name: Publish to npm
        run: npm publish
        env:
          NODE_AUTH_TOKEN : YOUR_NPM_TOKEN

Now just push the code on github to the appropriate branch and let github and CI/CD do the magic for you.

⚠️Note: Each time you publish the package, the version should be different in package.json, either it will be rejected by NPM.

You can check the package i created here, by running 

npm i byte-cli

This package can initiate a Node, Express and MongoDB project with a good directory structure by running

npx byte-cli init node

Alternatively if you already installed it using npm i, you can run

byte-cli init node

You can also check other supported commands using

npx byte-cli -h

This is all about publishing the first package on npm. Liked my idea? Follow me on Github

Published InJavaScript & NodeJs

A deep dive into modern JavaScript practices, frameworks, performance optimization, and clean coding techniques