Search code examples
javascriptnode.jsnode.js-stream

Check if a stream is process.stdout


Is there an elegant way to determine if a stream is process.stdout

I am working with streams and will like to end the stream, but found out that if the stream is process.stdout an error is thrown because process.stdout is a special stream that cannot be closed. So want to end all streams except it's process.stdout

I tried using a try and catch, but the process.stdout error ends the node process, ignoring the try and catch.


Solution

  • Perhaps this is naive of me, but I'd think you can just check with !==:

    if (theStream !== process.stdout) {
        theStream.end();
    }
    

    Streams are objects, and there's only one process.stdout, so...