Global objects in Node.js are available in different parts of a program without requiring us to import them explicitly. They provide useful features for working with values, timers, environment information, and more.
JavaScript running in Node.js provides several globally available objects and functions. Some common examples are:
console – displays messages and debugging information.process – provides information about the current Node.js process.global – refers to the global object in Node.js.setTimeout() – schedules a callback to run after a delay.setInterval() – schedules a callback repeatedly.Buffer – works with binary data.URL – represents and parses URLs.
The console object is used to display messages,
errors, warnings, and debugging information in the terminal.
console.log("Hello Node.js");
console.warn("This is a warning");
console.error("This is an error");
console.table([
{ name: "Amit", age: 20 },
{ name: "Rahul", age: 22 }
]);
Output examples:
Hello Node.js
This is a warning
This is an error
The console.table() method displays the
supplied data in a table-like format. Warning and error
messages may have different visual styling in the terminal.
The process object provides information about the
currently running Node.js process. It can be used to read
environment variables, access command-line arguments, and
inspect runtime information.
console.log("Node.js version:", process.version);
console.log("Operating system:", process.platform);
console.log("Process ID:", process.pid);
console.log("Current folder:", process.cwd());
Possible output:
Node.js version: v22.x.x
Operating system: win32
Process ID: 12345
Current folder: C:\Projects\NodeApp
The global object is the global object in Node.js.
Properties added to it can be accessed throughout the same
JavaScript environment.
global.appName = "My Node App";
console.log(global.appName);
Output:
My Node App
Although this works, avoid adding unnecessary properties to the global object because they can cause naming conflicts in larger applications.
In a CommonJS JavaScript file, __dirname contains
the absolute path of the directory containing the current file.
console.log(__dirname);
Possible output:
C:\Projects\NodeApp
__dirname is
available directly in CommonJS files. It is not
automatically defined in ES Modules. ES Modules can
derive the current file path using
import.meta.url.
In CommonJS, __filename contains the absolute path
of the current JavaScript file, including its filename.
console.log(__filename);
Possible output:
C:\Projects\NodeApp\app.js
Remember the difference:
__dirname gives the directory path,
while __filename gives the full file path.
Both are CommonJS-specific variables.
The setTimeout() function schedules a callback
to execute after a specified delay in milliseconds.
console.log("Start");
setTimeout(function() {
console.log("This message appears later");
}, 2000);
console.log("End");
Output order:
Start
End
This message appears later
The delay is 2000 milliseconds, or approximately two seconds. The callback may run later than the specified delay if the event loop is busy.
The setInterval() function repeatedly runs a
callback after each interval until the interval is cleared.
let count = 1;
const timer = setInterval(function() {
console.log("Count:", count);
count++;
if (count > 3) {
clearInterval(timer);
}
}, 1000);
Output:
Count: 1
Count: 2
Count: 3
The clearInterval() function stops the
repeated execution of the interval callback.
A Buffer is used to work with binary data, such as
data read from files or received over a network.
const data = Buffer.from("Hello");
console.log(data);
console.log(data.toString());
Output:
<Buffer 48 65 6c 6c 6f>
Hello
Buffer.from() converts the text into a
buffer. The toString() method converts
the buffer's contents back into text.
The global URL class allows us to create and
inspect URLs without importing a separate module.
const website = new URL("https://example.com:8080/products?id=10");
console.log(website.hostname);
console.log(website.port);
console.log(website.pathname);
console.log(website.searchParams.get("id"));
Output:
example.com
8080
/products
10
| Object or Function | Purpose |
|---|---|
console |
Display messages and debugging information. |
process |
Access information about the current Node.js process. |
global |
Access the Node.js global object. |
setTimeout() |
Schedule a callback after a delay. |
setInterval() |
Schedule repeated callback execution. |
Buffer |
Work with binary data. |
URL |
Parse and work with URLs. |
__dirname |
Directory of the current file in CommonJS. |
__filename |
Full file path in CommonJS. |
console.log().process.version.process.cwd().setTimeout()."Soopro Pathshala" and print it as text.Hint:
const data = Buffer.from("Soopro Pathshala");
console.log(data.toString());
Select the correct answer for each question.
1. Which object provides information about the current Node.js process?
2. Which function schedules a callback to run after a delay?
3. Which object is used to work with binary data in Node.js?