Node.js Tutorial

Node.js Global Objects

Node.js Global Objects
Node.js Global Objects

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 file
  • console ÔÇö logging and debugging
  • process ÔÇö info about the running process
  • Buffer ÔÇö handling binary data
  • setTimeout / setInterval / setImmediate and 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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us