Friday, April 17, 2020

Node.js global objects

Node.js global objects

Node.js Global objects
Node.js Global objects


One of the major differences between Node.js and JavaScript is the globals. Despite the fact, that the Node is built over the JavaScript itself still Node has a totally different purpose. For example, in-browser JavaScript has a window object with global, that is, of course, missing in Node because Node is not designed to deal with the browser window. So, Node has its own global objects. These objects are global in nature and available in all modules. We are not required to include these objects externally in our application.

Another major difference is that In browsers JavaScript, the top-level scope is the global scope. This means that in the browser if we declare a variable as,

var myvar 

Then it will define a new global variable, but in Node.js environment the story is different. If a variable is declared as,

var myvar inside a Node module, then it is not the global scope, it will be local to that particular module.

Many modules, functions, strings, and objects, etc. are available as global objects in Node.js.

A list of Node.js global objects are given below:


  • buffer
  • __dirname
  • __filename
  • require()
  • console
  • exports
  • global
  • clearImmediate(immediateObject)
  • clearInterval(intervalObject)
  • clearTimeout(timeoutObject)
  • module
  • process
  • queueMicrotask(callback)
  • setImmediate(callback[, ...args])
  • setInterval(callback, delay[, ...args])
  • setTimeout(callback, delay[, ...args])
  • TextDecoder
  • TextEncoder
  • URL
  • URLSearchParams
  • WebAssembly

The global object

Any Node Script has one variable named global that is accessible globally and referred to as a global object. This object has properties like global.console, global.require and global.process. All of the first level properties can be directly accessed without using global.

global.console.log("Welcome");

The __filename

This object represents the file name of the file from which the code is being executed. 

console.log(__filename);
/*
Output:
C:\Users\my\Desktop\brackets\test.js
*/

The __dirname

The __dirname object is used to represent the directory name in which the currently executing Node script is placed.



console.log(__dirname);
/*
Output:
C:\Users\my\Desktop\brackets
*/


The setTimeout(callback, delay)

The setTimeout(callback, delay) is a global function. The callback is executed after the given delay in milliseconds. However, the practical delay time is dependent upon some factors such as OS timer granularity and system load. A time interval cannot be set larger than 24.8 days.

The setTimeout returns an opaque value that represents the timer which can be used to clear the timer.

function printMessage() {
        console.log( "This message will be printed after some delay!");
     }

     // Let the delay be set as 2 seconds

     setTimeout(printMessage, 2000);
/*
Output:
This message will be printed after some delay!
*/

The buffer object

An instance of the Buffer class can be used to store the raw data similar to an array of integers (from 0 to 255) and corresponds to a raw memory allocation outside the V8-heap. The size of the buffer is fixed and it cannot be changed once it is created. The Buffer class can be accessed in any application without importing the buffer module as it is a global class.

var buffer = Buffer.from("Welcome to Node.js", "utf-8");
console.log(buffer);
console.log(buffer.toString());
/*
Output:
<Buffer 57 65 6c 63 6f 6d 65 20 74 6f 20 4e 6f 64 65 2e 6a 73>
Welcome to Node.js
*/

The clearTimeout(timeoutObject)

The clearTimeout(timeoutObject) is a global function. This function is used to halt the timer previously set with setTimeout() global function. The timeoutObject is the timer returned by the setTimeout().

function printMessage() {
        console.log( "This message will be printed after some delay!");
     }

     // Let the delay be set as 2 seconds
     var time = setTimeout(printMessage, 2000);

     // clear the timer
     clearTimeout(time);

The setInterval(callback, delay[, ...args])

This function is used to run the callback after a fixed time delay passed into the function. This function repeatedly executes the callback. This time delay cannot be larger than 24.8 days.

The setTimeout returns an opaque value to represent the timer by which it can be used to clear the timer.


function printMessage() {
        console.log( "This message will be printed repeatedly after some delay");
     }

     // This message will be kept printed repeatedly after 2 seconds time-gap

     setInterval(printMessage, 2000);
/*
Output:
This message will be printed repeatedly after some delay
This message will be printed repeatedly after some delay
This message will be printed repeatedly after some delay
This message will be printed repeatedly after some delay

*/

The interval can be cleared using clearInterval() function.


Reference:https://nodejs.org/api/globals.html