Create your first Node Module and publish it to the NPM registry

Installing Node Modules through NPM

NPM is the Node Package Manager, it makes it easy to install external libraries and frameworks for use in your own projects. With NPM installed you can simply type:

npm install <em>module-name</em>

Upon running this command, npm will fetch that module from the repository and place it within the “nodemodules” directory within your project. You can also install modules globally by adding the -g argument, installing a module globally places the code within the “nodemodules” directory where the node binary is installed.

Steps to create your own public npm module

  1. Create a new repository on github and clone it locally
  2. Create a package.json file to describe your module
  3. Create and Test the code for your module
  4. Publish your module to NPM

Start creating your first NPM module

To create your own module, you’ll want to have a public repository on GitHub to house the source code. After cloning your new module repository to your local machine, run:

npm init

This will walk you through a set of prompts that allows you to specify details about your new module. NPM Init

Most of these details are pretty obvious, and for many of the prompts you can just select the defaults. Entry point is the javascript file that will be invoked when consumers of your module “require” it, this file will include the main logic for your module, or if it is a large module you can export public functions found with other files (typically in the lib directory). After filling in the required information, a package.json file will be created with all the entered details

Create and Test the code for your module

Obviously, you’ll want your node module to do something useful, so start building out the functionality your module will provide. For my sample module, let’s say I want to make a function to calculate the distance between two points, my index.js file might look like this:

exports.dist = function(p1, p2){
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
};

Next we can create a number of files to test the code we’ve created. For the dist function I’ve defined I can create a file to test it with the following code:

var myMod = require('./../index.js');
var p1 = {x:1, y:1};
var p2 = {x:4, y:5};
var dist1 = myMod.dist(p1, p2);
console.log("Distance between: ", p1, " and ", p2, " is:", dist1);

In the package.json file I specified the following command to run for the test script:

for f in tests/*; do echo "$f"; node "$f"; done; echo 'passed!'; exit 0

This command will be executed when I run:

npm test

Which should output something like this:

tests/test.js
Distance between:  { x: 1, y: 1 }  and  { x: 4, y: 5 }  is: 5
passed!

Publish your module to NPM

Now that you’ve written and tested the code for your node module, it’s time to publish it to the npm registry. Before you can publish your new module you’ll need to register a user on the npm website, or you can create a user with the adduser npm command. Finally you are ready to publish your module, simply issue the command:

npm publish

You will be prompted for the credentials for the user you just created and if all goes well, your module will be available for other to install using the npm install command:

npm install <i>module-name</i>

When making changes to your module you’ll have to update the version number in the package.json file and then reissue “npm publish” this will ensure users of your module can get the latest code.

Now that you’ve published your module you and others can leverage that useful code in other node projects!

References

npm developers guide How to Module

A personal blog by Matt Palmerlee about software development, coding, leadership, and much more. Checkout my resume.)
Feel free to reach out to me using these channels© 2019