Publish Your First Node Module


  • Install Nodejs.
  • Create an account on GitHub & NPM.
  • Basic knowledge of JS & git.

NPM configuration

After installing nodejs and creating npm account, go to the terminal and run these commands.

npm set "Pradeep Khileri"
npm set ""
npm set ""

Let’s create our node module

  • before naming your module, check if there is already a node module of same name!.
  • here our sample node module’s objective is to extract json data of python package from pypi registry.

  • Create project dir and follow these steps.
      mkdir pkgstat #let us name our project 'pkgstat'
      cd pkgstat
      npm init # do as it says and it will create package.json(a configuration file for your node module).
  • Install dependencies for our project. we will use got module which will be used for making http request and extracting json content from web.
      npm install --save got # this will be installed locally,used only for current project.  
  • Read more about installing npm packages globally without sudo.
  • Write the code for our module in index.js
      'use strict';
      const got = require('got');
      const pypi = "";
      function extractMetaData(jsonData, language){
          var pkgMeta = {};
          if(language == 'python'){
              pkgMeta.description =;
              pkgMeta.url =;
              pkgMeta.source =;
              pkgMeta.license =;
              pkgMeta.version =;
          pkgMeta.status = 200;
          return pkgMeta;
      function setpkgURL(name,language){
          language = language.toLowerCase();
          var url = "";
          if(language == 'python'){
              url = pypi.replace('pkg_name', name);
          else url = "language not supported"; //'got' will handle invalid http request.
          return url;
      module.exports = (name,language) => {
          const url = setpkgURL(name, language);
          const headers = {'User-Agent':'got node module'};
          return got(url, {json:true, headers})
                  return extractMetaData(resp.body, language);
              .catch(err =>{
                  return {status:404};

Writing Test for our Module

  • testing is important because otherwise you have to manually test it again and again in node(repl) after making some changes in module.
  • other developers can also test your module with npm test without headache.
  • it’s a good habit, so shut up and write tests for your node module.
  • you can use mocha, ava or any other node testing module.(we will use ava).
      npm install -g ava
      ava --init #make sure you are in our project directory where our module resides.
  • write your test in test.js
      import test from 'ava';
      import pkgstat from '.';
      test('cleanslate pkg in python(pip)', async t =>{
          const data = await pkgstat('cleanslate','python');
,'Pradeep Khileri');//matching response(since we know the returned json object(data) contents).
      test('somePkgWhichDoesNotExist in SomeLanguageWhichDoesNotExist', async t =>{
          const data = await pkgstat('somePkgWhichDoesNotExist', 'SomeLanguageWhichDoesNotExist');
, 404);
  • run this test script.
      npm test # it will show which test was passed and which one didn't 
  • write documentation for your node module.
    • guide on writing markdown.
    • write your documentation in file.
    • this is sample documentation of our node module.
  • final project structure(more or less) will look like this.

Publishing our node module.

  • Publish your module to NPM (make sure you’re in project directory).
    npm login 
    # fill out username,password and email
    npm publish
  • Create a github repo and push your code.
  • you don’t want to push node_modules/ to github because these dependencies will be installed when someone will install your module.
  • use tool like GiG for generating .gitignore files for your project.
  • read more about github.

Final Note



Leave a Comment