Creating Node Modules

Part 1 - Building

Posted by Joel Lord on November 18th, 2015

Recently, I published my first modules on npm. At first, the process was a bit intimidating. I wasn't sure how to proceed, what to include and so on. This series of posts will guide you through the process of building and publishing your own npm module.

What's a module

Let's start with the basics. What exactly is a Node module? Well, modules are a way to organize your code in units that are easily reusable. Whatever code you extracted to rewrite as a function is a candidate for a module. The first thing that you'll need is an idea for a module. In our examples, we create a module that returns random numbers.

Creating and including a module

Node uses CommonJS as the pattern for modules. This means that to include your module, you will need the "require" keyword and inside your module, you will need to "export" your functions.

//randomNumber.js
//A simple function that returns a random number
function randomNumber() {
  return Math.random();
}

module.exports = randomNumber;

//index.js
//Require the randomNumber.js file
var random = require("./randomNumer");
//Store a random number in aNumber
var aNumber = random();
//Ouput to the console
console.log(aNumber);

And that's all you need to create your first node module. You can now reuse this module in all your projects where you need to return a random number.

Going one step further

In this example, we used a function as the object that was exported by the module. You could export just about anything. In the following example, we will show how to export an object that would have private variables

//randomNumber.js

//Create private variables.  Those will not be
//accessible by the index.js file
var min, max;
var public = {};

//Expose a function to set the minimum random
//value
public.setMin = function(value) {
  min = value;
}

//Expose a function to set the maximum random
//value to be returned
public.setMax = function(value) {
  max = value;
}

//Generate a random number between min and max
//If no min or max values were defined, the
//random number will be between 0 and 100.
public.generate = function() {
  if (min === undefined) min = 0;
  if (max === undefined) max = 100;
  return (Math.random()*(max-min)) + min;
}

//Return the object for use in index
module.exports = public;

//index.js
//Require the module the say way
var random = require("./randomNumber");

//Use the object to set min, max and then
//generate a new number
random.setMin(5);
random.setMax(10);
//Output a number between 5 and 10
console.log(random.generate());
//Output undefined because min is not
//accessible by index
console.log(random.min);

Your first module

And that's it ! If you're like me, you can never remember the formula to generate a random number between x and y (or just too lazy to think hard enough to figure it out). Now you have a module that you can plug in any project to generate this number for you.

Next steps

In the next posts, I will describe how to test this module and how to use npm to create a package that is ready to get published (part 2).