Node.JS Domain Module

Unhandled errors can create some serious problems for the programmers. Node.js domain module detects and terminates these errors. It can use either external or internal binding to accomplish the task. The Node application will ultimately crash if you fail to handle these errors for an extended period of time.

  • Internal Binding: In this type of binding, error emitter executes its code inside domain’s run method.
  • External Binding: In this type of binding, you can use the added method to add error emitter exclusively to a domain.

You can use the following syntax to import this particular module.

var domain = require(“domain”)

The node.js domain module has its own domain class. The functionality of both undetected exceptions as well as routing errors is provided to active Domain object by this very domain class. It is generally known as EventEmitter’s child class. The basic function of the class is to catch errors. You must keep an eye on its error event to effectively handle these errors.

The easiest way to use any domain is to run this function inside the domain. Subsequently, you have to explicitly bind all low level requests, timers as well as EventEmitters generated within that domain.


In this particular method, you will implicitly add an error emitter to a domain. It works similarly to implicit binding. The code will route all the error events and errors generated by emitters and emitter handlers to the main error event of the domain.


This method is exact opposite of domain.add. In this method, you remove domain handlers from a certain emitter.


domain.bind and domain.intercept work almost on the same principle. However, domain.intercept also catches Error objects. These errors act as the first argumentation to the function.


This method removes the current domain from domain heap by exiting the domain. It is imperative to exit the current domain whenever you have to execute a different series of asynchronous calls. The method also calls to exit input/output operations of a domain as well as delimits at the beginning of a series of asynchronous calls.

Take a look at the following example to see some of the aforementioned functions in action.

var EventEmitter = require("events").EventEmitter;
var domain = require("domain");

var emit1 = new EventEmitter();

var dom1 = domain.create();

dom1.on('error', function(err){
   console.log("Error handled by dom1 ("+err.message+")");


   console.log("listener handled this error ("+err.message+")");

emit1.emit('error',new Error('Listener will handle this'));


emit1.emit('error',new Error('Dom1 will handle this'));

var dom2 = domain.create();

dom2.on('error', function(err){
   console.log("Error handled by dom2 ("+err.message+")");
   var emit2 = new EventEmitter();
   emit2.emit('error',new Error('Dom2 will handle this'));   

emit1.emit('error', new Error('Exception message...!'));

Download the code Try the code

<<< Node.JS DNS ModuleNode.JS Packaging >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .