Home >>Nodejs Tutorial >Node.js Global Objects

Node.js Global Objects

Node.js Global Objects

Global objects Node.js are available in all modules and are global in nature. You don't have to include these objects in your application; you can use them directly instead. These objects are modules, functions, strings, objects and so on. In reality, some of these objects are not in the global scope but in the scope of the module.

A list of Node.js global objects are given below:

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

Node.js __dirname

This is a string. It specifies the directory name which the code currently contains.

File: global-example1.js
console.log(__dirname);

Open the command prompt Node.js and run the code below:

node global-example1.js

Node.js __filename

It specifies the code being executed as the filename. This is the absolute path of this code file which is resolved. The value within a module is the path to the file for that module

File: global-example2.js
console.log(__filename);

Open the command prompt Node.js and run the code below:

node global-example2.js