
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 Name | Description |
---|---|---|
1 | console | Prints information to stdout and stderr. |
2 | process | Retrieves 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
Leave a Reply