Let vs Var vs Const

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.

Example :

let a = 'hello'; // globally scoped
var b = 'world'; // globally scoped
console.log(window.a); // undefined
console.log(window.b); // 'world'
var a = 'hello';
var a = 'world'; // No problem, 'hello' is replaced.
let b = 'hello';
let b = 'world'; // SyntaxError: Identifier 'b' has already been declared

Node Js : Accepting Command Line Argument

Time zone Example :

node init
touch index.js
node install moment-timezone
node index.js Asia/Kolkata


const moment = require('moment-timezone');


let targetTimezone;

if(process.argv.length != 3){
    console.log("Usage: node <script-file> <timezone>")

console.log(`Time zone at ${targetTimezone} timezone is ${moment().tz(targetTimezone).format()}`);

Node Modules

  • Node modules are added into a npm project after running command “npm install <package>”
  • “node_modules” folder is created inside the parent folder.
  • under node_modules we will have a directory for each package
  • Under this package folder inside node_modules you will have these directories and files
    • src directory
    • min directory
    • locale directory
    • dist directory
    • package.json file
    • etc
  • Note, node_module folder is always heavy and we do not provide it or commit it, it should automatically be downloaded at the system in which source code is compiled using command “npm install”
  • npm install command will download the node_modules based on package.json and package-lock.json or if you provide a package name at the end of the command(point 1)

npmrc file in npm

  • It is a rc configuration file which is stored in users directory
  • We can execute npm config command to set the values in this file
  • Location of npmrc file “~/.npmrc”
npm config set init.author.name Tyson
npm config set init.author.email gill.tyson332@gmail.com

Sample .npmrc file


package.json in NPM

  • package.json file is created after performing npm init
  • package.json file contains the npm configuration for that project

Sample : package.json file

  "name": "demo",
  "version": "1.0.0",
  "description": "Demo of 3rd Part demo packages",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  "author": "Tyson Gill",
  "license": "ISC"
  • start in above script points the entry point of the npm project, where it states that if start command is fired for this npm project what to do, in our case we execute command “node index.js”. It is typially a bash script, you can add any command in this place. We can execute it using command “npm run start”
  • test is a second life cycle script of npm just like start, its purpose is it execute text scripts

NPM – Node Package Manager

NPM – Use to stand for Node Package Manager, but not currently.

  1. NPM is an online repository where all the 3rd party packages are loaded
  2. It is a command line utility, where it reads package.json file and download all the dependencies required for that project.
  3. NPM is managed by a company called NPM INC
  4. packae.json is a setting file of npm that helps you configure and setup the project

Check is npm is using below command :

npm -v

Initialize a NPM project :

npm init

Now package.json file is created, which will be contain all the dependency, packae.json is a setting file of npm that helps you configure and setup the project

It will look like below :

Using “npm run start” command you can execute the command “node index.js” and “npm start” is the short hand for “npm run start”

Download a dependency into your project and add it into package.json

npm install <package_name>
Example : 
npm install moment
  • Once the command is executed successfully
  • The entry of this js will go inside package.json inside “dependencies” section

Read Me :

Package.json : https://docs.npmjs.com/cli/v6/configuring-npm/package-json

Require an API

Example : Reading user input

const readline = require('readline'); //Read Line API Call

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout

rl.question('Enter your name ', (answer) => {
  console.log(`Hello ${answer} , Welcome to node JS`);


List of API’s Below

Node JS API’s of node Js : https://nodejs.org/api/index.html

Export and Require in Node JS

  • Everything you write in node is a module
  • You can export a function is a module using “module.exports”
  • You can use a exported module using “require”
  • Example below


function add(a, b){
    return a+b;
function subtract(a,b){
    return a-b;
module.exports.add = add;


mathObject = require("./MathsDemo.js");
var output =  mathObject.add(10,20);
console.log('Result is : '+output);

Output :

Example : Writing data to a file

const fs = require('fs');
fs.writeFile('myfile', 'Hello World', (error)=>{
    if(error) {
        console.log('Error while saving data');

Example : Taking user input and storing data to file

const readline = require('readline'); //Read Line API
const fs = require('fs'); //File System API

const wirteToFile = (data) => {
    fs.writeFile('myfile', `${data}`, error => {
        if(error) {
            console.log('Data has been saved');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout

rl.question('Enter data ', (data) => {