a blog for those who code

Saturday, 18 March 2017

How to create a NPM package

In this post we will be discussing about creating a NPM package and publish it. We will be writing a simple NodeJS program and then convert it to a NPM package. We will be creating a Indian Pincode NPM package which gives us the information about the pincode. This will be useful for people who wants to target all the areas in India.

ALSO READ : Getting Started with Node Package Manager

At first we will be creating a file named index.js. The contents of the file is shown below where we are reading a .csv file and then returning a callback with all the information of that pincode. Now to make the function searchByPin to be available to other files we have to export the function using exports.functionName.
var fs = require('fs');
var csv = require('fast-csv');
var result = [];
exports.seachByPin = function (pin, callback) {
  if (pin.length < 6 || pin.length > 6)
    return callback("Invalid PinCode");
    var csvStream = csv.fromPath("pinCode.csv").on("data", function (data) {
      if (data[1] === pin) {
          PinCode : data[1],
          OfficeName : data[0],
          DivisionName : data[4],
          RigionName : data[5],
          Taluk : data[7],
          DistrictName : data[8],
          StateName : data[9]
  }).on("end", function () {
      return callback(result);
Full Code can be found at GitHub : https://github.com/codingdefined/pincode

Next thing is to create a package.json file (to set different parameters for your package) and a readme file (to let user know about your package).

  "name": "pincode",
  "version": "1.0.0",
  "description": "Node module to easily loopkup Indian PinCode Information",
  "author": "Coding Defined <codingdefined@gmail.com> (http://www.codingdefined.com)",
  "main": "index.js",
  "dependencies": {
    "fast-csv": "0.6.0"
  "repository": {
    "type": "git",
    "url": "https://github.com/codingdefined/pincode.git"
  "keywords": [
    "india pin code"
  "license": "MIT",
  "bugs": {
     "url": "https://github.com/codingdefined/pincode/issues"
   "homepage" : "https://github.com/codingdefined/pincode"
After having all the files you have to publish the package. To publish the package just run npm publish command. After successfully pulishing you can check your package on npm. If your package is live then its successfully published. Our pincode package can be found at https://www.npmjs.com/package/pincode.

If you are getting warning NodeJS - npm WARN package.json : No repository field while installing your npm package then refer How to Solve No repository field Warning in Node.js.

Please Like and Share the CodingDefined.com blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us


Email *

Message *


Best Hosting Plans

CC License