Open In App

Node.js Global Objects

Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source project that can be used for server-side scripting. Node.js Global Objects are the objects that are available in all modules. Global Objects are built-in objects that are part of the JavaScript and can be used directly in the application without importing any particular module. The Node.js Global Objects are listed below: 
 

1.Class: Buffer The Buffer class is an inbuilt globally accessible class that means it can be used without importing any module. The Buffer class is used to deal with binary data. Buffer class objects are used to represent binary data as a sequence of bytes. 
 

  • console: It is an inbuilt global object used to print to stdout and stderr. 
     

  • process: It is an inbuilt global object that is an instance of EventEmitter used to get information on current process. It can also be accessed using require() explicitly. 
     

  • global: It is a global namespace. Defining a variable within this namespace makes it globally accessible. 
     
var myvar

2.It is a global scope when declared within the browser. However, any variable defined within a node.js file is accessible only within that file.

  • setImmediate() method: It schedules the immediate execution of the callback. The callback functions are queued and executed in the order in which they are created. The callback queue is processed at every event loop iteration. If there is a timer queued inside the executing callback, the timer will not get triggered until the next event loop iteration.
  • clearImmediate() method: It stops the immediate object returned by the setImmediate() method.
  • setInterval() method: It executes the callback function at repeated intervals. If an interval is larger than 2147483647 or less than 1, the interval is set to 1. Non-integer delays are truncated to the nearest integer.
  • clearInterval() method: It stops the interval object created by setInterval() method. 
     
  • setTimeout() method: It is a global function used to run a callback function after at least delay in milliseconds. Node.js does not guarantee the exact timing of when callbacks will fire but tries to maintain the timing as close as possible to the specified delay. Any delay larger than 2147483647 or less than 1, is set to 1 automatically. Non-integer delays are truncated to the nearest integer. 
     
function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
var timeoutObj = setTimeout(printHello, 2000);

  • clearTimeout() method: The clearTimeout() method is used to cancel or stop a timeout that was set with setTimeout() method. The timeoutObj is the object returned by setTimeout() method. 
     
  • queueMicrotask() method: A microtask is a short function that is executed after the callback function exits and only if the JavaScript execution stack is empty. The queueMicrotask() method is used to execute such functions after the callback function completes successfully. If the callback function does not return the control to other JavaScript code, the event loop runs all of the microtasks in the microtask queue. The microtask queue is processed multiple times per iteration of the event loop. If a microtask adds more microtasks to the queue then the newly-added microtasks execute before the next task is run. This is because the event loop keeps calling microtasks until there are none left in the queue.
  • TextEncoder: It is an implementation of the WHATWG Encoding Standard TextEncoder API. All instances of TextEncoder are encoded in UTF-8 only. 
     

3.TextDecoder: It is an implementation of the WHATWG Encoding Standard TextDecoder API. 
 

4.Class: URL The URL class instance is a global object and is implemented by the following WHATWG URL Standard. The URL constructor creates a new URL object as shown below. /foo is the input and https://www.helloworld.og/ is the base value. 
 

5.URLSearchParams: URLSearchParams API is used to perform read and write operations on the query of a URL. 
 

const myURL = new URL('https://www.register.com/?name=gfg');

// It prints gfg
console.log(myURL.searchParams.get('name'));

myURL.searchParams.append('name', 'xyz');

// It prints https://www.register.com/?name=gfg&name=xyz
console.log(myURL.href);

6.WebAssembly: The global object that acts as a namespace for all W3C WebAssembly related functionality. WebAssembly is a low level Assembly-like language that can be run on modern browsers. 

The following variables might appear to be global but actually exist only within the scope of some modules. 

  • require(id) method: It is used to import modules and returns an object of ‘any’ datatype.
var express = require('express')
  • exports: It is used to exports modules using module.exports.
  • module: It is a reference to the current module and is not global rather local to each module. It is used to make a particular module available through require() in the application. 
     
  • __dirname: The output throws an error which proves that __dirname is not globally defined in node.js. It requires a script to give the desired output as __dirname is only defined in scripts. 
     

  • Create a demo.js file
  • Paste the following code:
console.log("__dirname : "+ __dirname);
  • Run the demo.js file

7.__filename: The output throws an error which proves that __filename is not globally defined in node.js. It requires a script to give the desired output as __filename is only defined in scripts. 
 

  • Create a demo.js file
  • Paste the following code:
console.log("__filename : "+ __filename);
  • Run the demo.js file

 


Last Updated : 06 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads