Node.js Global Objects

Node.js Global Objects
Node.js Global Objects

Node.js Global Objects

When working with Node.js, you will often use global objects. These are built-in objects, functions, and properties that are available throughout a Node.js application without requiring an explicit import.

Some Node.js globals are truly available in the global scope, while others, such as __dirname and __filename, are provided within the module scope.

Node.js Global Objects
Node.js Global Objects

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Common Node.js Global Objects

  • __dirname – Provides the absolute path of the current directory.
  • __filename – Provides the absolute path of the current file.
  • console – Used for logging and debugging information.
  • process – Provides information and control over the current Node.js process.
  • Buffer – Used to work with binary data.
  • setTimeout(), setInterval(), and setImmediate() – Used to schedule asynchronous operations.

Node.js __dirname

The __dirname variable contains the absolute path of the directory where the currently executing JavaScript file is located.

// global-example1.js
console.log(__dirname);

// Run: node global-example1.js

This is especially useful when you need to work with files or folders relative to the current JavaScript file.

Node.js __filename

The __filename variable provides the absolute path of the currently executing file, including the file name.

// global-example2.js
console.log(__filename);

// Run: node global-example2.js

You can use __filename when your application needs to identify the exact file currently being executed.

Node.js Timers as Global Functions

Node.js provides timer functions that can be used directly without importing a separate module. These functions are useful for delaying execution, running code repeatedly, or scheduling callbacks.

setTimeout()

setTimeout() executes a callback function once after the specified delay.

setTimeout(() => {
  console.log('Runs after 2 seconds');
}, 2000);

In this example, the message is displayed after approximately 2 seconds.

Other Common Timer Functions

  • setInterval() – Executes a callback repeatedly after a specified time interval.
  • setImmediate() – Schedules a callback to run on a later iteration of the event loop.
  • clearTimeout() – Cancels a timer created using setTimeout().
  • clearInterval() – Stops an interval created using setInterval().
  • clearImmediate() – Cancels an immediate callback scheduled with setImmediate().

Download New Real Time Projects:- Click here

Final Thoughts

Node.js global objects and functions provide convenient access to commonly required features without the need for explicit imports. Understanding __dirname, __filename, console, process, Buffer, and timer functions will help you work more effectively with Node.js applications.

Learning these built-in features is an important step toward developing efficient and practical Node.js applications.

Keywords

Node.js Global Objects, Node.js global objects tutorial, Node.js globals, __dirname in Node.js, __filename in Node.js, Node.js process object, Node.js Buffer, Node.js timer functions, setTimeout Node.js, setInterval Node.js, setImmediate Node.js

Leave a Reply

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