Global Objects in Node.js

Node.js eliminates the need for explicit imports by enabling global objects to be accessed from anywhere in the application. Functionalities for managing files, streams, and processes are offered by these objects.

Node.js utilizes global as its global namespace, in contrast to browser-based JavaScript, which uses window as the global object.

Commonly Used Global Objects

Global

Access to built-in functions and objects is made possible throughout the application by the global object, which serves as the global namespace.

Example:

global.x = "Welcome to Node.js";
console.log(global.x);

Output:

Welcome to Node.js

Excessive use of global can lead to variable conflicts and maintenance challenges.

console

The console object is used for logging messages and debugging applications. It provides different methods:

  • console.log() – Outputs a standard message.
  • console.warn() – Displays a warning message.
  • console.error() – Shows an error message.
  • console.debug() – Used for debugging.

Example:

console.log("Hello");
console.warn("Warning!");
console.error("Error occurred");

Output:

Hello  
Warning!  
Error occurred  

process

The process object provides information about and control over the running Node.js process.

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 (Example Data):

Process ID: 12345  
Node.js Version: v22.13.1  
Command-line arguments: [ 'C:\\Program Files\\nodejs\\node.exe' ]  
Process Uptime: 5000 seconds  
Memory Usage: { rss: 43954176, heapTotal: 7614464, heapUsed: 6585976, external: 2339203 }  

__dirname and __filename

These global variables provide information about the location of the executing script.

  • __dirname – Absolute directory path of the script.
  • __filename – Absolute file path of the script.

Example (test.js):

console.log("Directory Name:", __dirname);
console.log("File Name:", __filename);

Output:

Directory Name: C:\Users\Lenovo  
File Name: C:\Users\Lenovo\test.js  

Timers

setTimeout

Executes a function after a specified delay.

Example:

setTimeout(() => {
    console.log("Executed after 5 seconds");
}, 5000);

Output (after 5 seconds):

Executed after 5 seconds  

setInterval

Executes a function repeatedly at a fixed interval.

Example:

let interval = setInterval(() => {
    console.log("Runs every 3 seconds");
}, 3000);

setTimeout(() => {
    clearInterval(interval);
    console.log("Stopped");
}, 10000);

Output:

Runs every 3 seconds  
Runs every 3 seconds  
Runs every 3 seconds  
Stopped  

Buffer

In Node.js, binary data is handled using the Buffer class. Because it permits direct memory manipulation, it is beneficial for networking, file systems, and streams.

Example:

let buffer = Buffer.alloc(10);
buffer.write("Node.js");
console.log(buffer.toString());

Output:

Node.js  

Other Common Global Objects

Sr. No.Module NameDescription
1consolePrints information to stdout and stderr.
2processRetrieves information about the current process and handles multiple events related to process activities.

It might be helpful:

Getting Started with Node.js: Your First Application

What’s New in Node.js 21/22? 

Best Practices for API Design in Full-Stack Development

What is REST API with an example?

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button