A guide to create nodejs npm package and publish on npm

Today we will discuss how to create an npm package and publish it on the npmjs from scratch. we will also create a test package to learn the complete process.

If you want to know more about the npm, its command and how it works, please read this article

As you already know about the package.json so first we will create it.

- Create package.json (npm main file)

let's create package.json file by running the npm init command, it will ask for some details like the package name, description, author. you can choose any name that you want. i used eavnitech-test01 as package name

you will see the below steps once you run the command.

D:\npm>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (pp) eavnitech-test01
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\npm\package.json:

{
  "name": "eavnitech-test01",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

now your package.json file will look like this

{
  "name": "eavnitech-test01",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

- Create index.js file

in the JSON file, you will see the main key as index.js so let's create a file as index.js, this file will be the main file that will be called once we import the npm package.

so let's add some code in the index.js file

- Add your logic in the package

In the index.js file please add below code

module.exports =  function(){
    var module1 = {}; 
  module1.additionFunction = function () {
        console.log('module 1 function called');
        return "Hello from module 1 function";
    };
  
  var module2 = {}; 
  module2.additionFunction = function () {
        console.log('module 2 function called');
        return "Hello from module 2 function";
    };
  
    return {module1, module2};
}

So from index.js, we are exporting the module1 & moddule2 modules of the packages, you can add as much module you want or simply can add a single module. I added 2 submodules and every module has a single function(you can add/remove functions as per your use-case)

Now package logic is ready and now times to import it.

- Import your module(Test your module)

For testing the module, You can simply create another file server.js and import the module like this. The code will be like this

const npmPackage = require('./index')();


console.log(npmPackage.module1.additionFunction()); 
console.log(npmPackage.module1.additionFunction()); 

now you can run node server.js and can able to test the package.

- Publish the package on npm(npmjs)

To release on npm, first, you need to create an account on the npmjs Please follow these steps to release the npm package

    1. Create a README.MD file on the root so this will be publish on your npm package page. this file is used to add the documentation of the npm package like the installation guide, module details.
    1. Now time to login into the npm through the command line, On the root folder, please run npm login,
      it will ask you to enter your username, password and email-id.
    1. Now run npm publish, the command will publish your package on the npmjs, after publishing go to npmjs and check your released package in your package list.

- Test the published npm package

Let's test the published package, please run npm i eavnitech-test01 , it will install the npm package that we developed Click to view

import the package by adding this line

const npmPackage = require('eavnitech-test01')();


console.log(npmPackage.module1.additionFunction()); 
console.log(npmPackage.module1.additionFunction()); 

Now we have learned how to develop the npm package, nodejs library and how to publish it on npmjs.

Please give it a try and I will be happy to answer your queries on my Twitter handle Twitter


Related blog of nodejs