What are Global Objects in Node JS?
Updated on May 15, 2025 | 7 min read | 8.95K+ views
Share:
For working professionals
For fresh graduates
More
Updated on May 15, 2025 | 7 min read | 8.95K+ views
Share:
Table of Contents
Did you know? Starting from Node.js 20 and onward, fetch has become a native global API in Node.js, eliminating the need for third-party HTTP clients like Axios or node-fetch for most use cases. This shift unifies server-side and browser JavaScript environments, making it easier than ever to write isomorphic code that runs seamlessly both in browsers and Node.js.
When building scalable and efficient applications in Node JS, understanding the environment you’re working in is essential. At the core of Node JS lie global objects, built-in features that give you direct access to key functionalities without requiring imports.
These global objects in Node JS simplify coding, manage asynchronous operations, handle system-level tasks, and interact with your application’s runtime seamlessly.
In this blog, we’ll dive into what are global objects in Node JS, how they work, and why mastering them is crucial for writing clean, high-performance server-side JavaScript.
Improve your understanding of global objects in Node JS with our online software development courses. Learn how to choose the right frameworks for your specific project requirements!
The global object in Node.js is called ‘global‘. It provides access to several built-in objects, including ‘process’, ‘console’, ‘buffer’, ‘setImmediate()’, ‘clearImmediate()’, and ‘setTimeout()‘, etc. For instance, the process object, an instance of EventEmitter, can be accessed from anywhere in the application, providing details about the running Node.js process. The console object prints to the standard output and standard error streams, while the buffer object handles data in binary format.
In contrast to Node.js, global variables specified in a web browser using the var keyword are not formed as members of the global objects in Node JS.
Node.js is a JavaScript runtime that enables developers to write client-side and server-side programmes in JavaScript without learning another language. It is neither a programming language nor a software development framework.
Thanks to this cross-platform and open-source JavaScript runtime environment, developers can run JavaScript code outside a web browser. It uses the V8 JavaScript engine that powers Google Chrome and other browsers.
Because of its practical and scalable design, Node.js is well-suited for developing fast and scalable server-side and networking applications. It uses an event-driven, non-blocking I/O (input/output) paradigm to handle multiple requests simultaneously without compromising future processing.
In 2025, professionals who can use frontend frameworks to streamline web applications will be in high demand. If you're looking to develop skills in frontend development, here are some top-rated courses to help you get there:
Here’s a code snippet that provides a basic example of how to define a Node.js server using the Express framework:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
This code creates a basic web server using Express.js that listens on port 3000. It handles GET requests to the root URL ('/') by sending back a simple "Hello, World!" message. When the server starts, it logs a message indicating it’s running and accessible at http://localhost:3000. This is a minimal setup to quickly serve web content.
Console Output when the server starts:
Server listening at http://localhost:3000
Browser or HTTP Client Output when accessing http://localhost:3000/:
Hello, World!
This means your server is up and running, and it responds to requests on the root URL with a simple greeting message.
Depending on the needs, one can create different codes to handle more routes, interact with databases, and carry out various other tasks in Node.js.
You can get to know more about Node.js with upGrad’s free Node.js For Beginners course. Learn to build scalable backend applications with this Node.js free course. Master Node.js fundamentals, architecture, and core modules.
Global objects in Node JS ease the retrieval of system-level data, the transfer of functionality between modules, and data access across modules.
Some of the common uses are:
If you want to build a higher-level understanding of Javascript, upGrad’s Advanced Javascript for All course is what you need. Learn about prototypes, scopes, classes, modules, async programming, and more to enhance your web development skills and become a proficient JS developer.
Also Read: Top 45+ Nodejs Project Ideas for Beginners and Professionals
Now that you know what are global objects in Node JS, let’s look at some of the commonly used global objects in Node JS.
Global objects in Node JS are pre-built objects that can be used in any module without including them in the application. These items are modules, functions, strings, and actual objects. While some items are accessible from anywhere, others are at the module’s level.
Here are some commonly used global objects in Node JS:
In Node.js, the console object writes data to the standard error (stderr) and standard output (stdout) streams. Console.log(), console.error(), console.warn(), and console.info() are among the methods in this library that display different forms of information. The console object is implemented at a lower level using the process.stdout.write() method.
Here’s an example of using the console object in Node.js:
console.log('Hello, world!');
console.error('This is an error message.');
console.warn('This is a warning message.');
console.info('This is an informational message.');
This code uses the console object to print different types of messages to the console. The output of this code would be:
Hello, world!
This is an error message.
This is a warning message.
This is an informational message.
Also Read: 10 Practical Uses of JavaScript for Every Developer
The process object in Node.js is a global object easily accessible from anywhere. It is an essential component of the Node.js ecosystem since it provides various data sets relevant to an application’s operation.
The process object, an instance of the EventEmitter class, includes built-in events like exit that can be used to determine when a Node.js application has finished executing. Furthermore, the process object has various other operational properties. Some of these can be incorporated into a Node.js application as a conduit between a command-line interface and the Node.js program.
Here’s an example of using the process object to get the current working directory:
console.log(`Current directory: ${process.cwd()}`);
This program uses the process.cwd() method to get the current working directory and prints it to the console.
Another example is obtaining the command-line arguments supplied to a Node.js program using the process.argv property:
console.log(`Command-line arguments: ${process.argv}`);
This program uses the process.argv property to get the command-line arguments passed to the Node.js program and prints them to the console.
Other attributes of the process object, including env, pid, title, uptime, and memoryUsage, can be used to obtain details about the running process.
Also Read: Node JS Versions: Which One to Download?
The Buffer object in Node.js directly manipulates binary data and can be created in various ways. Binary data is represented as a collection of bytes via the Buffer class. Creating a Buffer class instance can convert text into a binary data stream. An application can access the Buffer class without importing the buffer module.
Here’s an example of how to create a Buffer object in Node.js:
// Create an uninitiated Buffer of 10 octets
const buf1 = Buffer.alloc(10);
// Create a Buffer from a given array
const buf2 = Buffer.from([10, 20, 30, 40, 50]);
// Create a Buffer from a given string and optionally encoding type
const buf3 = Buffer.from('Simply Easy Learning', 'utf-8');
This code demonstrates three ways to create Buffers in Node.js. Buffer.alloc(10) creates a zero-filled buffer of 10 bytes. Buffer.from([10, 20, 30, 40, 50]) creates a buffer initialized with the specified byte values. Buffer.from('Simply Easy Learning', 'utf-8') creates a buffer containing the UTF-8 encoded bytes of the given string. Buffers are used for handling raw binary data efficiently in Node.js.
Output:
<Buffer 00 00 00 00 00 00 00 00 00 00>
<Buffer 0a 14 1e 28 32>
<Buffer 53 69 6d 70 6c 79 20 45 61 73 79 20 4c 65 61 72 6e 69 6e 67>
Also Read: Node.js vs JavaScript: Key Differences, Benefits & Use Cases Explained
In Node.js, the setInterval() method continually executes a function after a fixed delay. It produces a unique interval ID which the clearInterval can subsequently use to halt the function’s repeated future execution.
Here’s an example of using setInterval() and clearInterval() in Node.js:
function sayHello() {
console.log('Hello!');
}
// Call sayHello() every 1 second
const intervalId = setInterval(sayHello, 1000);
// Stop calling sayHello() after 5 seconds
setTimeout(() => {
clearInterval(intervalId);
}, 5000);
In this example, the sayHello() method is executed every 1 second using setInterval(). After 5 seconds, clearInterval() is called with the interval ID given by setInterval() to terminate the continued execution of the method.
The clearInterval() function accepts the interval ID returned by setInterval() as its input. If the argument supplied does not indicate an already existing action, this function performs nothing.
Output:
Hello!
Hello!
Hello!
Hello!
Hello!
If you want to improve your knowledge of object-oriented programming, upGrad’s Java Object-oriented Programming can help you. Learn classes, objects, inheritance, polymorphism, encapsulation, and abstraction with practical programming examples.
Also Read: Node JS vs Python: Difference Between Node JS and Python
Now that you’re familiar with global objects in Node JS, let’s look at how upGrad can help you learn Node JS.
The global objects by Node.js provide numerous capabilities and utilities, allowing developers to interface with the file system, handle events, manage modules, execute network activities, and more.
upGrad’s programs guide you from foundational to advanced Node.js skills with practical projects, real-world case studies, and expert mentorship. You’ll learn to leverage core global objects, asynchronous programming, and built-in modules to build efficient, scalable backend applications.
Here are some of the top upGrad courses (including free ones) to support your JavaScript Development journey:
For personalized career guidance, contact upGrad’s counselors or visit a nearby upGrad career center. With expert support and an industry-focused curriculum, you'll be prepared to tackle front-end development challenges and advance your career.
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://webcluesinfo.medium.com/global-objects-in-node-js-a-comprehensive-overview-24d1b83bf48e
900 articles published
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources