Global & Module Scope Objects in Node.js

The Tutorial provides the detailing about node.js global objects and the listing of node.js global objects and its usage. There are some objects which are considered to be global objects but actually they have scope within the module only. Let’s discuss in detail.

Node.js Global Objects

The below given is the list of node.js global objects which are available in all modules by default and does not need to include specifically in the application.

  • console
  • process
  • buffer
  • setInterval(callback, delay[, arg][, …])
  • setTimeout(callback, delay[, arg][, …])
  • clearImmediate(immediateObject)
  • clearInterval(intervalObject)
  • clearTimeout(timeoutObject)

Node.js objects with module scope

The below given is the list of node.js objects which has module level scope

  • __dirname
  • __filename
  • exports
  • module
  • require()

Node.js console class

The node.js console class can be read at the below given link

Node.js console Tutorial 

Node.js Buffer class

The node.js buffer class can be read at the below given link

Node.js Tutorial – Buffer Class

Node.js _dirname

The node.js _dirname is of type “string” and depicts the directory name of the current module.

The execution of node.js _dirname is similar to the execution of path.dirname of the _filename.

Example: Insert the below command and save the file as global_dirname.js

console.log(__dirname);

Node.js Global Object – _dirname example

Node.js _filename

The node.js _filename is of type “string” and returns the file name of the current module.

Node.js exports

The node.js exports is of type “object” and is available within a module’s file-level scope. Before the module is evaluated , the exports variable is assigned the value of module.exports.

The module.exports object is created by the Module system, In case it is required the module to be an instance of the class, then assignment of the object to the module.exports , rebinds the local export variable.

const alphaModule = require('./alphaModule');
alphaModule.on('ready', () => {
  console.log('module "alphaModule" is ready');
});

Assignment to module.exports cannot be done in callbacks and thus to be done immediately.

Example: late callback which does not work

const callbackObj = require('./callbackObj');
console.log(callbackObj.x);

Node.js module.id

The module.id depicts the identifier for the module. module.id is of type “string”

Node.js module.loaded

The node.js module loaded depicts if the module is done loading or in the process of loading. module loaded is of type “boolean”

Node.js module.parent

This is the first module to be required . The module.parent is of type “module”

Node.js module.path

The module.path depicts the search paths for the module

Node.js module.require(id)

The node.js module.require(id) allows to load the required module for which the reference of the module object is required. The require() returns module.exports object and the scope of the module is within the module code, thus module needs to be explicitly exported to be in use.

module.require(id) requires:

id : <string>

Returns: <exported module object>