Capturing stdout/ stderr in Node.js using Domain module

Gajus Kuizinas

This weekend I am working on a project that enables Applaudience developers to test multiple data aggregation scripts in parallel. Implementing this functionality requires that a single API endpoint evaluates multiple user submitted scripts. However, if either script fails, we need to retrieve the logs of the execution too, i.e. we need to capture what was written to stdout.

Capturing Node.js stdout and stderr output into a variable.

I have had this requirement before and I have already developed output-interceptor to solve it. It works by overriding process.stdout, e.g.

let output = '';

const originalStdoutWrite = process.stdout.write.bind(process.stdout);

process.stdout.write = (chunk, encoding, callback) => {
if (typeof chunk === 'string') {
output += chunk;

return originalStdoutWrite(chunk, encoding, callback);


process.stdout.write = originalStdoutWrite;

In the above example, output evaluates to foo\nbar\nbaz\n .

If your application processes all the tasks sequentially, then the above is all you need to capture program’s output. However, what if we need to capture output of multiple, concurrent asynchronous operations?

Turns out that we can create an execution context using domain. I admit that I knew of domain module, but never had a practical use case for it: I thought it is primarily used to handle propagation of asynchronous errors. Therefore, the capability to achieve the above was a pleasant surprise.

The trick is to override process.stdout.write and check for process.domain. process.domain is a reference to the current execution domain. If process.domain can be recognised as a domain that we have created with intent to capture the stdout, then we attach the intercepted stdout chunks to that domain, e.g.

const createDomain = require('domain').create;const originalStdoutWrite = process.stdout.write.bind(process.stdout);process.stdout.write = (chunk, encoding, callback) => {
if (
process.domain &&
process.domain.outputInterceptor !== undefined &&
typeof chunk === 'string'
) {
process.domain.outputInterceptor += chunk;
return originalStdoutWrite(chunk, encoding, callback);
const captureStdout = async (routine) => {
const domain = createDomain();
domain.outputInterceptor = ''; await => {
return routine();
const output = domain.outputInterceptor; domain.outputInterceptor = undefined; domain.exit(); return output;

In the above example, captureStdout captures everything that was written to process.stdout while executing routine. If there are multiple routines running concurrently, then their execution domain is used to distinguish their output.

Here is a working demo that you can play with.

If you need this functionality in your program, then consider using output-interceptor: I have since updated output-interceptor to handle asynchronous functions using the same principle as described in this article.

I figured this is worth sharing as it provides an example of creating and maintaining a reference to the execution context beyond handling asynchronous errors.

What do you use domain for?

A notice about “deprecation”

Several people commented that domain module is deprecated and it should not be used.

Deprecation notice

Despite the big red banner stating that this module is deprecated – domain module is not deprecated. If you read the paragraph following the banner, it states that the module is pending deprecation once a replacement API is finalised. It is likely that async_hooks will eventually provide all functionality provided by domain module and will supersede it. However, until that time it safe to use domain module.

Gajus Kuizinas

Written by

Software architect, startup adviser. Editor of Founder of

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade