How to publish a package to npm ?

To publish a package to npm, we should follow the following steps.

STEP 1 : Make sure your project have package.json file something like this.

STEP 2 : update your entry file with required functionality. Here entry file is index.js as we defined in package.json as “main”: “index.js”. Following is some example code.

STEP 3: signup to npm using https://www.npmjs.com/signup

STEP 4: Now login to npm using the following command in the terminal

STEP 5: publish the package using the following command. (Make sure package name is unique)

Finally you are able use that package anywhere by just installing with the following command