The Wayback Machine - https://web.archive.org/web/20240804041029/https://www.geeksforgeeks.org/node-js-global-objects/
Open In App

Node.js Global Objects

Last Updated : 06 Dec, 2021
Comments
Improve
Suggest changes
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

 


Previous Article
Next Article

Similar Reads

What are the global objects of Node.js ?
Node.js is a JavaScript framework based on an open-source project that is used for server-side scripting. Node.js Global Objects are those objects which are present in all modules. Global Objects can be used directly in the application which is available without importing any module. Global objects have been added keeping in mind that these are som
5 min read
What is the difference between Host objects and Native objects ?
In this article, we will learn about what are Host objects and Native objects, and their differences. JavaScript objects are broadly classified into 2 categories - native javascript objects and host javascript objects. Native objects: Native javascript objects are standard javascript objects which are provided by javascript itself. They are also kn
2 min read
Extract unique objects by attribute from array of objects
Given an array of objects and the task is to return the unique object by the attribute. Examples: Input: [ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 }, { name: 'Geeks', id: 20 }, { name: 'Geeks', id: 10 } ]Output:[ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 } ]Approach: Let's assume that name is an attribute that di
4 min read
How to Remove Null Objects from Nested Array of objects in JavaScript ?
Removing null objects from the nested array of objects can be done by iterating over the array, filtering out the null objects at each level, and then applying a recursive approach to remove all the null objects. This makes sure that all the levels of the nested structure are checked and the null objects are removed. There are various approaches to
7 min read
Filter Array of Objects with Another Array of Objects in JavaScript
Filtering an array of objects with another array in JavaScript involves comparing and including objects based on specific criteria. Below are the approaches to filter an array of objects with another array of objects in JavaScript: Table of Content Using filter and includes MethodsUsing LoopingUsing reduce MethodUsing Set for Efficient FilteringUsi
3 min read
How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has various methods to convert an array of objects into
8 min read
How to Update an Array of Objects with Another Array of Objects using Lodash?
Updating an array of objects with another array of objects involves merging or updating objects in the first array with corresponding objects from the second array. Below are the approaches to updating an array of objects with another array of objects in the Lodash library: Table of Content Using lodash _.merge() function Using lodash _.mergeWith()
4 min read
How to Convert Object Containing Objects into Array of Objects using Lodash?
Lodash is a JavaScript utility library that provides predefined functions to make code more readable and cleaner. These functions are optimized for performance, often being faster than native JavaScript methods for complex operations.We will learn how to convert an object containing objects into an array of objects using Lodash. There are two appro
3 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. There are various methods to achieve this separation
5 min read
Get list of Duplicate Objects in an Array of Objects
Finding duplicates in an array of objects is a common task in JavaScript, especially when dealing with data manipulation and validation. This will guide you through the process of identifying duplicate objects in an array using various techniques. Understanding the ProblemLet's assume we have an array of objects where each object has a set of key-v
4 min read
three90RightbarBannerImg