The Ultimate Guide to Publish an NPM Package (Step-by-Step)


Introduction

Hello folks, today I’m going to explain how to publish your Node package. NodeJS has got a very good stand for application developments. It became a new technology that everyone started to work with.

If you are a NodeJS developer, you might have noticed that during “npm install” it installs lots of packages. Well, that is the part of NodeJS, where third-party libraries are installed as a package.

Table of Contents


Step 1: Initialize Your NPM Package

The journey of writing an npm package starts with initializing npm in the dedicated directory of your package.

npm init

Provide all the details as asked. In the end, it will generate the file package.json.

package.json

Step 2: Create the Main File

Create a file named index.js, which will contain all the logic of your Node module.

exports.msg = function () {
  console.log("hello all!");
}

Step 3: Add a README File

You can provide details in the README.md file. This file is important to understand the purpose and important details of the module.

Step 4: Publish Your Module

To publish your module, execute the following command:

npm publish --access public

Note: Do remember that if you have not verified your email account on npmjs.com, it will not allow publishing your module.

Step 5: Update and Republish

After changing the logic of the module, publish it again with an updated version:

npm version patch

This will update the version of your Node module (for example, from 1.0.1 to 1.0.2). Then publish again using:

npm publish

Example Module

I have created a new module called json-diff-change, which you can find below: