Day 2: NodeJS - Node REPL and Modules

Photo by Sigmund on Unsplash

Day 2: NodeJS - Node REPL and Modules

Node REPL

REPL Stands for (Read-Eval-Print-Loop). It is a command line-based tool in which we can issue Node commands or JavaScript code

We can perform a variety of operations in this REPL tool. In Image 1 which is the one on the top, we can see that we can execute JavaScript code without any hassle and the fun part is, it runs anywhere. To enable REPL in VS-Code, just type "node" and hit enter.

We can also perform operations like saving our command line code in some files as well as loading a JavaScript file for execution in REPL.

.save <name_of_file>.js   // For saving the REPL Command line code in JS File
.load <name_of_file>.js   // loading file in REPL

When you finished your work with REPL, Press "Ctrl + D" or ".exit" to come out of it.

We can also load the JavaScript code file directly by specifying its path.

Modules

There are 3 types of Node Modules:

  • Core Modules (Provided by default by Node.Js)

  • Local Modules (will be developed by us)

  • Third-Party Modules (Downloaded from sites such as NPM)

Below are some of the examples and uses of Core Modules of Node.JS

    http           -> To create http server in node.js
    os             -> Provides info about operating system
    path           -> To handle the file path
    querystring    -> To handle url query string
    fs             -> To handle file system in the computer 
    util           -> To access utility functions
    url            -> To parse the url strings

Global Objects

In NodeJS, there are some special objects which we can access anywhere in our program code. They are known as Global objects. We can trigger the Global Objects as per our needs. Let's have a look at an example.

Suppose I want to obtain the file name and directory name of the file for which I am currently writing my JavaScript code. I will simply execute below code snippet:

console.log(__dirname)
console.log(__filename)

When I tried to execute this, I got the following output:

The first line gave the name of the directory we are currently in i.e. the directory from which we fired this Global Object. The second line gave us the name of the file from which the Global Object was written. In my case, I have named the file as "core-modules.js"

Path Module

Path module is usually used when we work with folder structures i.e. directories and files. When working with directories, a path module is very useful. To use Path Module we need to write the following line of code at the start.

const path = require('path')

The reason we didn't use any '.' or '/' in the location of the 'path module' is that it is a core module. It is always present in our program, we just need to import it.

Let us look at some of the methods provided by the path module.

// 1) basename -> gives the name of the main folder in which our program is
console.log(path.basename(__dirname))

// 2) dirname -> It will return the path of the folder in which we are currenlty. Have a look for reference of the path in below image
console.log(path.dirname(__dirname))

// 3) extname -> returns extension of the current file
console.log(path.extname(__filename))

OS Module

Os module is helpful when we are working on operating systems. We might run our programs on Windows, Mac, Linux, etc. By using Os Module, it may be able to provide some information about the operating system that the developer is currently using. To use it, we need to write this code statement at the start of our program:

const os = require('os')

Let's have a look at some of the methods provided by the Os Module:

// 1) arch -> returns the architecture of OS we are currently using
console.log(os.arch())

// 2) homedir -> returns the Home Directory on the System we are using
console.log(os.homedir())

// 3) platform -> returns the name of OS platform name which we are currenly using 
console.log(os.platform())

Third-Party Modules:

A Module that is installed via NPM (Node Package Manager) and is available online is known as Third Party Module.

EXAMPLE 1: - I want to import the "chalk-animation" package which is available online and can be installed via NPM. When the package is installed we can use it in our web application for different styles and purposes. To install the above Third-Party Module, we will fire up this command on our terminal:

npm i chalk-animation

We will require some JavaScript code now:

import chalkAnimation from 'chalk-animation';
chalkAnimation.rainbow('Welcome to Node.js Third Party modules...');
{
  "type": "module",
  "dependencies": {
    "chalk-animation": "^2.0.3"
  }
}

This is how your "package.json" should look after the installation of the above Third-Party Module. Now here is what we get as output:

EXAMPLE 2: - There is another Third-Party Module namely "validator" which validates everything from Email, Numbers, Strings, etc. To install it we are going to fire the following command:

npm i validator
import validator from "validator";
console.log(validator.isEmail("saketk@gmail.com"))
console.log(validator.isEmail("saketk#gmail.com"))

Have a careful look at how it validates on the basis of signs. Similarly, there are tons of exciting Third-Party Modules which are available on the site of npm which are easy to install and ready to use.

Therefore we learned today basics of NODE REPL to different types of Modules which we can use in our web applications.