Codementor Events

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 a...scotch.io
.

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+[https://github.com/f1lander/xtreme-math.git](https://github.com/f1lander/xtreme-math.git)"
      },
      "keywords": [
        "npm",
        "module",
        "package"
      ],
      "author": "f1lander",
      "license": "MIT",
      "bugs": {
        "url": "[https://github.com/f1lander/xtreme-math/issues](https://github.com/f1lander/xtreme-math/issues)"
      },
      "homepage": "[https://github.com/f1lander/xtreme-math#readme](https://github.com/f1lander/xtreme-math#readme)"
    }

    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.*/

       square(){
       
          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.

GitHub:


    npm install --save [https://github.com/f1lander/xtreme-math](https://github.com/f1lander/xtreme-math/tarball/master).git

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

    npm install --save [https://github.com/f1lander/xtreme-math](https://github.com/f1lander/xtreme-math/tarball/master)/tarball/master

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+[https://github.com/f1lander/xtreme-math.git](https://github.com/f1lander/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… stackoverflow.com.

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
post commentsBe the first to share your opinion
Rae-Win Tan
3 years ago

How do you test the project that use the xtreme-math package? I tried something similar(using classes in my package) when testing it in another project(let call it package tester) and run parcel index.html to serve the page of the package tester, i receive This experimental syntax requires enabling the parser plugin: ‘classProperties’ (2:14). I don’t want you to dwell to much in the parcel package as I did and I could not find an answer online(did it for a couple of days now my efforts were futile). Is there another way to test the package other that parcel? My package plays with the html i.e. it uses things like document.querySelector(“canvas”); Basically it is very frontend like. im not using any framework its just pure and simple javascript.

Show more replies