Node.js - Global Objects



Node.js is the popular runtime environment for executing the JavaScript outside the browser. One of its features are the availability of the global object, which provides the functionalities for building server-side applications.

In this tutorial we are going to explore the Node.js global objects, their significance and how they are used effectively in the applications.

Global Objects in Node.js

The Global Objects in the Node.js are accessible from anywhere in the application without needing to require or import them explicitly. These objects provides the functionalities such as working with files, streams and process the information.

Unlike the browser-based JavaScript, where the window object server as the global scope, Node.js uses the global as its global objects.

Commonly Used Global Objects

Let's look at the some of the most commonly used global objects in the node.js.

global

The global object is the global namespace in the Node.js. It allows to access the built-in functions and objects anywhere in the application without requiring the explicit imports.

However, the excessive use of the global object may lead to the variable conflicts and maintenance challenges.

Example
global.x = "Welcome To, TutorialsPoint.";
console.log(global.x);
Output
Welcome To, TutorialsPoint.

console

The console object is used to print the messages to the console and debug applications. It provides the various methods to log messages with different levels of importance, making it the crucial tool for the users.

console.log() − It outputs a standard message.

console.warn() − It displays a warning message.

console.error() − It shows an error message.

console.debug() − It is used for debugging purposes.

Example
console.log("Hello");
console.warn("Danger.!");
console.error("SyntaxError");
Output
Hello
Danger.!
SyntaxError

process

The process object in the Node.js is used to provide the detailed information and control over the running Node.js process. It allows the users to interact with the system environment, retrieve the process-related information and manage the execution behaviour.

Example
console.log("Process ID:", process.pid);
console.log("Node.js Version:", process.version);
console.log("Command-line arguments:", process.argv);
console.log("Process Uptime:", process.uptime(), "seconds");
console.log("Current Working Directory:", process.cwd());
console.log("Memory Usage:", process.memoryUsage());
Output
Process ID: 40096
Node.js Version: v22.13.1
Command-line arguments: [ 'C:\\Program Files\\nodejs\\node.exe' ]
Process Uptime: 5078.9264602 seconds
Memory Usage: {
  rss: 43954176,
  heapTotal: 7614464,
  heapUsed: 6585976,
  external: 2339203,
  arrayBuffers: 395682
}

__dirname and __filename

The __dirname and __filename are the two important global variables in Node.js that provides the information about the location of the currently executing script.

__dirname

  • It represents the absolute directory path of the script being executed.
  • It is used for constructing the file paths and handling the directories.

__filename

  • It represents the absolute file path of the script being executed.
  • It helps in logging, debugging and file management.

Let's create a folder 'test.js' and place the following commands in the file.

test.js
console.log("Directory Name:", __dirname);
console.log("File Name:", __filename);
Example
node test.js
Output
Directory Name: C:\Users\Lenovo
File Name: C:\Users\Lenovo\test.js

Timers

The Timers in the Node.js provides a way to execute the code after a delay or at the fixed interval. These are the part of the global objects and do not require importing any module.

setTimeout

It executes the function once after a specified delay and returns the Timeout object, which can be cleared by using the clearTimeout().

Example
let x = setTimeout(() => {
    console.log("Executed after 5 seconds");
}, 5000);
clearTimeout(x);
Output
Executed after 5 seconds

setInterval

It executes the function repeatedly at a fixed interval and returns the interval object , which can be cleared using the clearInterval().

Example
let z = setInterval(() => {
    console.log("Runs every 3 seconds");
}, 5);
clearInterval(z);
Output
Runs every 3 seconds
Runs every 3 seconds
Runs every 3 seconds......

Buffer

The Buffer class is used to for handling the binary data in the Node.js. It allows direct manipulation of the memory, making it useful for working with streams, file systems and networking.

Example
let x = Buffer.alloc(2);
x.write("TP, TutorialsPoint");
console.log(x.toString());
Output
TP

Global Objects

The following table provides a list of other objects which we use frequently in our applications. For a more detail, you can refer to the official documentation.

Sr.No. Module Name & Description
1 Console

Used to print information on stdout and stderr.

2 Process

Used to get information on current process. Provides multiple events related to process activities.

Advertisements