Search code examples
javascriptnode.jsevent-loop

Node.js: console.err() is syncronous or asyncronous?


I use AWS and I want write all errors to stderr using console.err(). stderr is caught by AWS Cloud Watch and logged. I don't want blocking the event loop.

Is console.err() in Node.js implementation asynchronous?


Solution

  • It depends on the OS and of where stderr is "going".

    From the Node.js documentation:

    process.stdout and process.stderr differ from other Node.js streams in important ways:

    1. They are used internally by console.log() and console.error(), respectively.

    2. Writes may be synchronous depending on what the stream is connected to and whether the system is Windows or POSIX:

      • Files: synchronous on Windows and POSIX
      • TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
      • Pipes (and sockets): synchronous on Windows, asynchronous on POSIX