Node.js Global Objects
Global objects are ready-made tools that Node.js gives every program. You use them without any import or install. Think of them as the free tools stored in every kitchen drawer.
The Global Toolbox
+-----------------------------------------------+ | Node.js Global Toolbox | +-----------+-----------+-----------+-----------+ | console | process | Buffer | timers | +-----------+-----------+-----------+-----------+ | __dirname | __filename| module | require | +-----------+-----------+-----------+-----------+
console
The console object prints messages in the terminal. You used it in your first program.
console.log("Normal message");
console.error("Error message");
console.warn("Warning message");
console.table([{ id: 1, name: "Asha" }, { id: 2, name: "Ravi" }]);The console.table method shows an array of objects as a neat table. This method helps when you inspect data.
process
The process object describes the running program. It holds facts about the program and gives you control over it.
Useful Properties
console.log(process.version); // Node.js version
console.log(process.platform); // win32, linux, or darwin
console.log(process.cwd()); // current working folder
console.log(process.argv); // command-line words
console.log(process.env.HOME); // an environment variableExit the Program
The process.exit method stops the program. Code 0 means success. Any other code means a problem.
if (!process.argv[2]) {
console.log("Please give a name.");
process.exit(1);
}Listen for Program Events
process.on("exit", (code) => {
console.log("Program ended with code " + code);
});__dirname and __filename
These two values tell your file where it lives. The __filename value holds the full path of the current file. The __dirname value holds the folder that contains it.
console.log(__filename); // /home/user/app/index.js
console.log(__dirname); // /home/user/app
/home/user/app/index.js
|__________________|____|
__dirname file name
|_______________________|
__filename
These values stay reliable even when you run the program from a different folder. Use them to build file paths.
Timer Functions
Timers run code later or repeatedly.
const id = setInterval(() => console.log("Tick"), 1000);
setTimeout(() => {
clearInterval(id);
console.log("Stopped");
}, 3500);- setTimeout runs a function once after a delay.
- setInterval runs a function again and again.
- setImmediate runs a function right after the current work finishes.
- clearTimeout and clearInterval cancel a timer.
global
The global object holds everything available in every file. You can attach your own value to it.
global.siteName = "eStudy247";
console.log(siteName);Avoid this habit in real projects. Shared global values make bugs hard to track. Use modules to share data instead.
Module-Level Helpers
Four helpers look global but belong to each file: require, module, exports, and the two path values above. Node.js wraps every file in a function and passes these helpers in.
Your file
+--------------------------------------------+
| function (exports, require, module, |
| __filename, __dirname) { |
| |
| ...your code lives here... |
| |
| } |
+--------------------------------------------+
This wrapper explains why a variable in one file stays private from other files.
Other Handy Globals
- Buffer stores raw binary data.
- URL and URLSearchParams read and build web addresses.
- fetch sends web requests in modern Node.js versions.
- structuredClone makes a deep copy of an object.
Practice Tasks
- Print your Node.js version and operating system.
- Print the folder name of your current file.
- Start an interval that prints a number every second and stops after five ticks.
