Building a Utility Class as a Module (npm)

Published Oct 13, 2017
Building a Utility Class as a Module (npm)

First, we need to have an idea that they are npm packages. Of course, if someone is building an application in Node.js, I imagine that they know, but they can visit the following link to know how to start with Node.js.

01 - What is npm? | npm Documentation
The place where all things npm are documented

Here's a tutorial on how to make a RESTful API:

Build a RESTful API Using Node and Express 4
With the release of Express 4.0 just

11 - Creating Node.js modules | npm Documentation
The place where all things npm are documented

Normally, when we want to use some kind of utility in our Node app, we use functions defined in a file and then export them as a utility class to use it where we need it.

This becomes a bit complicated and is not a good practice if you need to use this class in several projects.

In our module, we have two important files: index.js and package.json. With the command npm init, we created our package.json as shown below:

mkdir xtreme-math && cd xtreme-math
    npm init 

      "name": "xtreme-math",
      "version": "1.0.0",
      "description": "Mi primer paquete",
      "main": "index.js",
      "scripts": {
        "test": "npm test"
      "repository": {
        "type": "git",
        "url": "git+[]("
      "keywords": [
      "author": "f1lander",
      "license": "MIT",
      "bugs": {
        "url": "[]("
      "homepage": "[]("

    Is this ok? (yes)

The file package.json defines the file to which our module points:

    "main": "index.js"

We'll export all the classes that we want to use.

We'll also create a .js file that will contain our class:

touch math.js 

Now, in the file that we just created, we'll make our class that will contain the functions:

    "use strict"; 

    class Math {

       constructor(number) {

           this.number = number;


       /* This function will return the square of the number that the constructor of this class receives.*/

          return this.number * this.number;



    module.exports = Math;

Once we create our class that contains the square function, we have to export the class as a module and then into the index.js file that we define as the package main.

    const math = require("./math");

    module.exports = math;

Easy, right?

After we have our package ready to use as a module in another project, we can do the installation directly from our repository in GitHub or locally.


    npm install --save [](

    /* note: if they want to install from a specific branch for example "master" */

    npm install --save [](

To install the local file package, you must edit the package.json and add the file path to the dependencies.

    "dependencies": {
        "xtreme-math": "file:/Users/Doom/Desktop/xtreme-math"

You will notice that in your package.json file, we added the package we just installed.

    // github

    "dependencies": {
        "xtreme-math": "git+[]("

    // archivo local previamente editado.

    "dependencies": {
        "xtreme-math": "file:/Users/Doom/Desktop/xtreme-math"

Note: this type of class that does not depend on another dependency can be used as a "devDependencies." Here is a link explaining the differences:

What's the difference between dependencies, devDependencies and peerDependencies in npm package…
Summary of important behavior differences: Related options not discussed here: dependencies are required to run…

Once done, we can make use of our xtreme-math package in several projects.

An example would be:

    "use strict";

    const xtreme_math = require("xtreme-math");

    const number = 2;

    const math = new xtreme_math(number);

    console.log(`The square of ${number} is: ${math.square()}`);

    //The square of 2 is: 4

I hope this small tutorial has been useful for you.

Edax Uclés.

Discover and read more posts from Edax Filander Ucles Hernandez
get started