Open In App

Node.js Global Objects

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. 
 





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.

function printHello() {
   console.log( "Hello, World!");
}

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

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. 

var express = require('express')

console.log("__dirname : "+ __dirname);

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. 
 

console.log("__filename : "+ __filename);

 

Article Tags :