How To Create a Node.js Module that gets Covid-19 data

Introduction

Why we’re using Node.js: Node.js came into existence  because we can only run JS in a browser and wanted it to run on your machine as a standalone application.

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. using non-blocking I/O

What is a module? In Node.js, a module is a collection of JavaScript functions and objects that can be used by external applications

Prerequisites

  • You will need Node.js and npm installed on your development environmeny
  • node –version:  v12.16.1 (for this tutorial)
  • By having Node.js installed you will also have npm installed;
  • this tutorial uses version  node -version 6.13.7.
  • You should also be familiar with the package.json file, and experience with npm commands would be useful as well. To gain this experience, follow
  •  Step 1 — Creating a package.json File.
  • It will also help to be comfortable with the Node.js REPL (Read-Evaluate-Print-Loop). You will use this to test your module. If you need more information on this, read our guide on How To Use the Node.js REPL.

Step 1 — Creating a Module

This step will guide you through creating your first Node.js module. You will use the Node.js built-in exports property to make the function and array available to external programs.

In your terminal, make a new folder called covid-data-module and move into it:

  • mkdir covid-data-module
  • cd covid-data-module

 

Overview§

  1. Create a package.json file
  2. Create the file that will be loaded when your module is required by another application
  3. Test your module

 

Create a package.json file§

  1. To create a package.json file, on the command line, in the root directory of your Node.js module, run npm init:
  2. Provide responses for the required fields (name and version), as well as the main field:
    • name: The name of your module.
    • version: The initial module version. We recommend following semantic versioning guidelines and starting with 1.0.0.
    • main: The name of the file that will be loaded when your module is required by another application. The default name is index.js.

Initialize npm so other programs can import this module later in the tutorial:

  • npm init -y

You used the -y flag to skip the usual prompts to customize your package.json.

Output

  "name": "covid-data-module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Create and implement your module in index.js
~/covid-data-module/index.js
 
const getCovidStats = async() => {
    try {
        const response = await fetch('https://covidtracking.com/api/us');
        const usa = await response.json();
        console.log(usa);
    }
    catch (err) {
        console.log(`Error: ${err}`);
    }
  
};
exports.printStats = function (){
    getCovidStats();  
}

 

Once your package.json file is created, create the file that will be loaded when your module is required. The default name for this file is index.js.

In the file, add a function as a property of the exports object. This will make the function available to other code:

The exports keyword references a global object available in every Node.js module. All functions and objects stored in a module’s exports object are exposed when other Node.js modules import it.

Save and exit the file.

Publish Module to NPM

If you don’t have an npm account – Create one below:

https://www.npmjs.com/signup

Login using your credentials:

npm login

After logging in, you’re ready to publish!

npm publish

 

Test your Module

Create another directory

mkdir TestMyModule

Switch into the directory

cd TestMyModule

Create your test script – I called mine test.js

var mymodule = require('covid-data-module') 
mymodule.printStats()

Create the test node project

npm init

Install your created module

npm install covid-data-module

Execute the test script

node test.js

Sources: https://www.digitalocean.com/community/tutorials/how-to-create-a-node-js-module

https://docs.npmjs.com/creating-node-js-modules#create-the-file-that-will-be-loaded-when-your-module-is-required-by-another-application

Published by anthonykuong

Anthony is a versatile Software professional with around 10 years of experience. He is a Full Stack developer experienced with clients in the Financial, Health and Supply Chain industries. He is experienced with MVC frameworks ( Spring Boot) , SPA frameworks ( Angular , VueJS), and also supports automated build deployments and packaging for development, qa, and production servers.. He has delivered rich user experience using Modern web technologies and techniques such are HTML5, CSS3, ECMAScript 6 (ES6)/ ECMAScript 2015, CSS pre-processors (SASS, Less), JavaScript build tools (Grunt, Gulp) , various UI Frameworks including AngularJS , Knockout JS , and CSS Frameworks including Bootstrap, and Foundation. He is adaptable to new technologies and frameworks. He is a rigorous, quality-conscious contributor with solid analytical skills. I can also be found on youtube - Youtube Channel: https://www.youtube.com/user/akuong/

Leave a comment