I have created one node package which i explained in previous tutorial. That package is published at Node package manager site and few people have downloaded that as well. In this tutorial i am going to explain how to create a NodeJS NPM package and publish it at npmjs.org.
Directory Structure :
Yes this is it. Our main code will be living at index.js file. Let us create the package.json file because for Node module it is very very important.
Creating Package.json :
Here is package.json file which i have created for our node calculator.
"description": "This package will return addition of two numbers",
"name": "Shahid Shaikh"
"readme": "# Add two numbers (for learning purpose)nnDemo module for codeforgeek.com tutorialnn## Installnn npm install nodecalcnnn## APInn### -calc(a,b,p) n where, n a=number,nb=number,p=operator",
"_id": "[email protected]",
"_from": "[email protected]"
Coding the actual module : NodeCalc
Create new file as “index.js” in lib folder and place this code in it.
return "Please provide all parameters";
exports.calc=calc; //very important line
You know what this is it. Now what we need to do is to publish this module at Node repository.
Publish Package to NPM:
First go to npmjs.org and create new account. Its simple and easy. Once you do it, open up your terminal and type following command.
It will ask your user name, password and email. You can even create new account from here too. But doing it before is helpful. Here is screen shot for help.
Once you did this step. What you need to do is publish it.
After this step here is my module page at npmjs.org.
Installing Nodecalc module and testing it:
Let’s test our module. Create any folder and switch to that folder using terminal and type.
Once done check whether node_modules is created on working folder or not. It should be there, now create any JS file and paste following code.
Run it using node and you should get following output.
If you are facing some error, you might need to check whether the “nodecalc” module is been installed correctly or not.
Edit published Module at NPM:
Suppose you want to update some information about this module, so what you need to do is unpublish the module, change the proper information for.eg Readme information and then change version number ( This is very important else you will face error) and then publish it again.
Unpublish the module:
In case you want to update the module information or simply delete it from npm registry, all you need to do is.
from the working folder. That’s it.