Node.js Global Objects

Node.js Global Objects

When working with Node.js, you’ll often come across global objects. These are built-in objects available across all modules in a Node.js application. The best part? You don’t need to explicitly import them—they can be used directly.

These global objects include modules, functions, strings, and objects that simplify working with Node.js. However, note that some of these objects are not strictly in the “global scope” but exist within the module scope.

Introduction to Applied AI:-Click Here

Common Node.js Global Objects

Here’s a list of widely used global objects in Node.js:

  • __dirname
  • __filename
  • Console
  • Process
  • Buffer
  • setImmediate(callback[, arg][, ...])
  • setInterval(callback, delay[, arg][, ...])
  • setTimeout(callback, delay[, arg][, ...])
  • clearImmediate(immediateObject)
  • clearInterval(intervalObject)
  • clearTimeout(timeoutObject)

Data Science Tutorial:-Click Here

Node.js __dirname

__dirname is a string representing the absolute path of the directory that holds the currently executing script.

Example (global-example1.js):

console.log(__dirname);

Run this script in Node.js:

node global-example1.js

This will print the directory path of your file.

Download New Real Time Projects :-Click here

Node.js __filename

__filename provides the absolute path of the file being executed. Inside a module, it returns the complete path to that module file.

Example (global-example2.js):

console.log(__filename);

Run this script:

node global-example2.js

You’ll get the exact file path of the script being executed.

Machine Learning Tutorial:–Click Here

Node.js Console

For a detailed explanation of the Console class in Node.js, check here:
Node.js Console – updategadh

Final Thoughts

Node.js global objects make coding easier by providing direct access to useful values and methods without requiring imports. As you build applications, getting comfortable with these will save you both time and effort.

Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:– Click Here


node js global variable across files
node-glob
node js global variable example
global objects in javascript
nodejs globalthis
global object in javascript example
structuredclone nodejs
modules in node js
node js global objects list
node js global objects examples
node js global objects geeksforgeeks
node js global objects github

Share this content:

Post Comment