How to Build and Publish a NPM Package

I have created one node package which i explained in previous tutorialThat 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

Before building the Node module you need some JavaScript skills. But let’s make it simple, we will develop one simple module which will take two numbers and operator(+,-,*,/) as input and print the result accordingly. Pretty simple to code in JS but let’s do it in Node.js.

Directory Structure :

----CFG npm module

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.

"name": "nodecalc",
"version": "0.0.1",
"description": "This package will return addition of two numbers",
"main": "./lib/index.js",
"repository": {
"type": "git",
"url": ""
"keywords": [
"author": {
"name": "Shahid Shaikh"
"readme": "# Add two numbers (for learning purpose)nnDemo module for tutorialnn## Installnn npm install nodecalcnnn## APInn### -calc(a,b,p) n where, n a=number,nb=number,p=operator",
"readmeFilename": "",
"dependencies": {},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
"bugs": {
"url": ""
"homepage": "",
"_id": "[email protected]",
"scripts": {},
"_shasum": "db9a913c2f8893d477472647917493f2c0949adb",
"_from": "nodecalc@"

I am sure you will say WTF is this. You will come to know the importance of each lines when we publish the app. Now let’s move ahead and create simple module in JavaScript which accept two number and operator and return the result.

Coding the actual module : NodeCalc

Create new file as “index.js” in lib folder and place this code in it.

var calc=function(a,b,p) {
    if(a==="" || b==="" || p==="") {
        return "Please provide all parameters";
    switch(p) {
        case '+': {
            return a+b;
        case '-': {
            return a-b;
        case '*': {
            return a*b;
        case '/': {
            return a/b;
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 and create new account. Its simple and easy. Once you do it, open up your terminal and type following command.

sudo npm adduser

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.

Screenshot from 2014-08-25 18:17:33

Once you did this step. What you need to do is publish it.

sudo npm publish

After this step here is my module page at


Installing Nodecalc module and testing it:

Let’s test our module. Create any folder and switch to that folder using terminal and type.

npm install --save nodecalc

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.

var math=require("nodecalc");

Run it using node and you should get following output.nodecalc2

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 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.

npm unpublish

from the working folder. That’s it.



Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Articles: 298