Node.js Global Objects
When working with Node.js, you will often come across global objects. These are built-in objects available across all modules in a Node.js application. The best part ÔÇö you do not need to explicitly import them; they can be used directly. Note that some of these objects are not strictly in the global scope but exist within the module scope.
Node.js Tutorial:-
Complete Python Course:-
Common Node.js Global Objects
__dirnameÔÇö absolute path of the current directory__filenameÔÇö absolute path of the current fileconsoleÔÇö logging and debuggingprocessÔÇö info about the running processBufferÔÇö handling binary datasetTimeout/setInterval/setImmediateand their clear functions ÔÇö timers
Node.js __dirname
__dirname is a string representing the absolute path of the directory that holds the currently executing script.
// global-example1.js
console.log(__dirname);
// Run: node global-example1.js
Node.js __filename
__filename provides the absolute path of the file being executed, including the file name.
// global-example2.js
console.log(__filename);
// Run: node global-example2.js
Timers as Global Objects
setTimeout(() => {
console.log('Runs after 2 seconds');
}, 2000);
Download New Real Time Projects:- Click here
Complete Advance AI topics:-
Final Thoughts
Node.js global objects make coding easier by providing direct access to useful values and methods without requiring imports. Getting comfortable with __dirname, __filename, process, and timers will save you time. For more Node.js guides, stay connected with .
node js global variable across files
global objects in javascript
nodejs globalthis
global object in javascript example
modules in node js
node js global objects list
node js global objects examples
node js __dirname __filename