Write your own Node Modules

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

Node Package Manager

Node Modules


If you have worked with Node or NPM earlier, you must have seen a CLI (Command Line Interface) command like

npm install something or  npm install something -g or npm install -g something

And you notice that after this runs, all the files related to  something will be downloaded to your node_modules folder? Fascinating!!  So what is this NPM?


NPM – Node Package Manager

From wiki

npm is the official package manager for Node.js. As of Node.js version 0.6.3, npm is bundled and installed automatically with the environment.[1] npm runs through the command line and manages dependencies for an application. It also allows users to install Node.js applications that are available on the npm registry.

npm is written entirely in JavaScript, and runs on the Node.js platform.

You can find a comprehensive list of all Node Packages here. This post will guide you in writing a simple NPM module, that you will host on npmjs.org so that others can consume it.

There are 2 parts to the complete process

  1. Create and Publish a node module
  2. Consuming the module in another application

Create and Publish a Node module

  • Step 1 : Create and clone a Github repository
  • Step 2 : Create a package.json and include dependencies
  • Step 3 : Create test cases for your code
  • Step 4 : Create an account with NPM (one time) and publish your module

Step – 1

  • Go to https://github.com/
  • Create a new account
  • Create a new repository https://github.com/new
  • Get the git clone url (Should be on the right hide side of the project main page) and clone it to a folder (myModule)
  • ex:[email protected]:arvindr21/npm-calculator.git

Step – 2

Create a package.json and include dependencies
  • Typically to build a node module, you need Node as well as npm
  • You can check this post to help you setup Node.
    • Alternatively, You can head to http://nodejs.org/ and click on the shiny green Install button – the set up is fairly simple
    • Then open a terminal/command prompt and run
      npm -v  to verify the installation
  • Next, open folder: myModule, open/navigate your terminal/prompt here & execute npm init. This will ask a set of questions and Bam!! you have your package.json file

Note: We are creating a calculator node module

Create a file (index.js) at the root of the folder, and add the below code to index.js. This is our node module.

Our Module so far

Step – 3

Create Test cases and validate our module
  • As you can see from the sample package.json earlier, we have added a “devDependencies” section
  • Lets set that up. Back to terminal/prompt and run “npm install”
  • As you can see, this has added our jasmine-node testing framework to our module
  • Create a folder called “specs” at the root and add a file called“calculator-spec.js”
  • This is where our test cases reside

Jasmine test cases for the calculator module. Copy the below to calculator-spec.js

Execute the created test cases
  • Back to terminal/prompt, execute “npm test”
  • This will let you know, if everything is fine or not. Ideally it should say
    “5 tests, 5 assertions, 0 failures, 0 skipped”

Step – 4

Create a npmjs account
  • Go to https://npmjs.org/signup and create an account
  • Come back to the terminal/prompt and execute
    “npm adduser”
    provide your credentials and then
    “npm publish”
  • You can go to https://npmjs.org/~ and your package will be listed at the bottom of the page. (login if needed)

Thats it. Share  npm install npm-calculator with your friends and family! Or Else create a Github page and mention the details!

Steps to use the created NPM Module

  • Step 1 : Create a folder in which you want to install the NPM Module
  • Step 2 : Install Node (if not already done)
  • Step 3 : execute ‘npm init’ –  Follow the steps above to create a package.json
  • Step 4 : Add the package and name it as a dev dependency
  • Step 5 : Execute ‘npm install’
  • Step 6 : Leverage the library for your application

Note: If you are testing your package, create a new folder called “myCalcApp” and follow the above & next steps. Update your terminal/prompt path too.

Package.json file for the calc app

Create a file at the root and name it myApp.js. Then add the below code.

  • Save both the files and the run  npm install to install the calculator app and its dependencies
  • Then execute  node myApp.js
  • The output would be  9 

Thats it, we are all done. You can use this approach as a base to write super awesome node modules!

Thanks for reading! Do comment.

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page