NodeJS Global Objects
In NodeJS global objects are the objects that are accessible in the application from anywhere without explicitly using the import or require.
- In browser-based JavaScript, the window object is the global scope, meaning it holds all global variables and functions.
- In NodeJS, instead of the window object, the global object serves as the global scope, which holds all the global variables and functions for the application. This is how NodeJS manages things that are available throughout the entire app, just like how Windows does in the browser.
Explore the NodeJS Global Object Complete Reference for detailed explanations, usage examples, and expert tips. Master these essential tools to streamline your NodeJS development and manage your application’s environment effectively.
Commonly used Global Objects in NodeJS
NodeJS provides a set of global objects that are available in every module. These are built-in objects that can be used directly in the application. Let’s take a look at some of the most commonly used NodeJS global objects
1. global
The global object in NodeJS is equivalent to the window object in browsers. Any variable or function added to global becomes globally accessible across the application.
global.a = 'This is a global variable';
console.log(a); // Accessible from anywhere in the application
Output
This is a global variable
2. console
The console object is used for printing messages to standard output (stdout) or error output (stderr). It provides methods like console.log(), console.error(), and console.warn() for logging messages and debugging.
console.log("This is a log message");
console.error("This is an error message");
Output

3. process
The process object in NodeJS provides information about the currently running NodeJS process. It allows you to interact with the system, get details about the process, and control how the process runs. In simple terms, it helps you manage things like environment settings, command-line arguments, and how the application behaves during execution.
console.log("Process ID:", process.pid);
console.log("Node.js Version:", process.version);
Output
Process ID: 32 Node.js Version: v16.20.1
4. Buffer
The Buffer class is used to deal with binary data in NodeJS. It provides a way to handle raw binary data directly in memory, allowing you to manipulate binary files or network streams.
const buffer = Buffer.from('Hello Node.js');
console.log(buffer); // Outputs the binary representation
Output
<Buffer 48 65 6c 6c 6f 20 4e 6f 64 65 2e 6a 73>
5. __dirname and __filename
These are global variables that represent the directory name (__dirname) and the filename (__filename) of the current module (file).
console.log(__dirname); // Outputs the directory of the current file
console.log(__filename); // Outputs the full path of the current file
Output
/home/guest/sandbox /home/guest/sandbox/Solution.js
6. setTimeout() and setInterval()
These functions are used to schedule the execution of code. setTimeout() runs a function after a specified delay, while setInterval() runs a function repeatedly at fixed intervals.
setTimeout(() => {
console.log("This runs after 2 seconds");
}, 2000);
setInterval(() => {
console.log("This runs every 3 seconds");
}, 3000);
Output

7. URL and URLSearchParams
URL is used to handle URL-related operations, and URLSearchParams helps with manipulating URL query parameters.
const myURL = new URL('https://www.example.com/?name=anjali');
console.log(myURL.searchParams.get('name'));
myURL.searchParams.append('age', '30');
console.log(myURL.href);
Output
anjali https://www.example.com/?name=anjali&age=30
8. TextEncoder and TextDecoder
These classes are used for encoding and decoding text in various encodings, such as UTF-8. They are useful for working with string data that needs to be converted to binary or vice versa.
const encoder = new TextEncoder();
const encoded = encoder.encode("Hello, Node.js!");
console.log(encoded); // Outputs a Uint8Array of encoded text
Output
Uint8Array(15) [ 72, 101, 108, 108, 111, 44, 32, 78, 111, 100, 101, 46, 106, 115, 33 ]
Why Are Global Objects Useful?
Global objects in NodeJS are especially useful because they allow you to access commonly used functionalities without needing to import them from external modules. For example, instead of needing to require a module to interact with the file system, you can use the fs global object to directly manipulate files and directories.
When to Use Global Objects in NodeJS?
Global objects are helpful in situations where you need to access commonly used functionalities across your application. They are particularly useful for:
- Managing the process: The process object gives you access to process-related information and the environment.
- Working with file paths: The __dirname and __filename objects help determine the directory structure of your application, which is useful for handling relative file paths.
- Timers and intervals: The setTimeout() and setInterval() functions are helpful for controlling the flow of your application.
- Handling binary data: The Buffer object allows you to work with binary data efficiently.
When NOT to Use Global Objects?
While global objects are convenient, they should be used with caution. Over-relying on global objects can make your code harder to test and maintain. Here are some cases when you should avoid using global objects excessively:
- Namespace Pollution: Using too many global variables can cause naming conflicts, making it difficult to manage your code and increase the risk of bugs.
- Hard-to-Track Dependencies: When code relies on too many global variables, it becomes harder to determine where certain variables are being defined or modified, which makes the code harder to debug and understand.
- Testing Challenges: Code that relies heavily on global objects can be difficult to mock or stub during testing.
Conclusion
Global objects in NodeJS provide access to important functionality, making your development process more efficient and streamlined. While these objects are useful for tasks like file handling, process management, and interacting with the environment, it’s important to use them responsibly. Overusing global objects can lead to harder-to-maintain code and difficulties in debugging and testing.