Publishing your NPM package - An Overview

Publishing your NPM package - An Overview
- 15th Mar, 2020 | 4min read

Publishing a package to the npm registry so that it can be installed by name and can be used by many other developers around the globe.

In this article we cover publishing a demo package used for very basic functionality


You should know few things before proceeding ahead

Before we start, make sure you have an account on npm, if not make one now.

Creating a package

First create a folder, in this case demo-hello-world-pkg and move to the newly created directory

cd demo-hello-world-pkg

Now initialize your package

npm init -y

Well, this package is already installed in the npm registry, with mentioned details in package.json file, So you just modify it with your own.

If you are not publish it on a github repository, then skip the highlighted once. otherwise replace it with your own

  "name": "demo-hello-world-pkg",
  "version": "1.0.0",
  "description": "\" Demo package - Just for test\"",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": ["test","demo"],
   "repository": {      "type": "git",      "url": ""    },    "bugs": {      "url": ""    },  "author": "rahul",
  "license": "ISC",
  "homepage": ""

Now create a index.js file in the same directory

// To print a massage on console
exports.printMsg = function() {
  console.log("This is a demo package");

// To return 'Hello World' string
exports.hello = function() {
  return "Hello World";

// To return current date = function() {
   return Date();

Before publishing

Now we have to login to our npm account and for that we have to use

npm login

Now it will ask for your NPM account Username, Password and a public email of your account after login. When you enter your Password it will not show on screen, so don't panic, Just enter your correct account password and press enter

Email: (this IS public)
Logged in as xxxxxx on


We have two ways for that

  • unscoped - To publish unique package in the entire npm registry
  • scoped - To publish unique or similar package associated with your account or your Organization


Remember in unscoped, package name must be unique in the entire npm registry (like your username) If you want the use same name as already their then use scoped publishing

For unique package - search the package name on npm registry, if their is an exact match then it will show else you found a unique package name, now you can use that

Publishing unscoped is very easy just type in your package root directory

npm publish


Publishing packages associated with your or your Organization's account

You have to made a little change in package.json file

  • For a user-scoped package, replace my-scopeName with your userName
  • For a Org-scoped package, replace my-scopeName with your OrgName
  "name": "@my-scopeName/demo-hello-world-pkg",

Publishing scoped is also very easy now, just type in your package root directory

npm publish

By default, scoped packages are published with private visibility. To publish a scoped package with public visibility, use

npm publish --access public

See your published package

Now visit your account to see your published package, replace my-username with your username

Now you can create a project in NodeJs and try Using your npm package

Now you can logout your account

npm logout

Updating Package

Well for updating, first make your changes and then update a bit in package.json file with updating next version number like

  "name": "demo-hello-world-pkg",
  "version": "1.0.1",

Before publishing you must login into your account. Now you can follow the publishing same as before


In this article we have learned how to publish our own package

Now you can create one and share among your friends