Node: TypeError: Console expects a writable stream instance

Created on 16 Jun 2018  Â·  7Comments  Â·  Source: nodejs/node

my code:

const fs = require('fs');
const { Console } = require('console');
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');

// custom simple logger
const logger = new Console({ stdout: output, stderr: errorOutput });
// use it like console
const count = 5;
logger.log('count: %d', count);

computational results:

TypeError: Console expects a writable stream instance

This is why?

console question wrong repo

Most helpful comment

Oh, stupid me. As I pointed out, Console(opts) doesn't work in Node v8.x, so changing the line which says:

const logger = new Console({ stdout: output, stderr: errorOutput });

to

const logger = new Console(output, errorOutput);

would fix everything. That said, the above code would work perfectly from Node v10.x onwards.

Closing this because it seems like your problem must be fixed, but feel free to reopen if you feel otherwise.

All 7 comments

@Q-Angelo What Node.js version do you use? This Console constructor is available since Node.js 10.0.0.

@vsemozhetbyt IIRC, the Console(options) constructor was added in v10.0.0, while Console(stdout[, stderr][, ignoreErrors]) existed earlier.

@Q-Angelo okay, this shouldn't happen because fs.createWriteStream should definitely return streams that are Writable.

A slightly more dreadful test POC:

const {createWriteStream} = require('fs');
const { Console } = require('console');
const { Writable } = require('stream');

const output = createWriteStream('./stdout.log');
const errorOutput = createWriteStream('./stderr.log');
console.log(output instanceof Writable);
console.log(errorOutput instanceof Writable);

// custom simple logger
const logger = new Console({ stdout: output, stderr: errorOutput });
// use it like console
const count = 5;
logger.log('count: %d', count);
> $ node 21366.js                                                                                                       ⬡ 8.11.1
true
true
console.js:35
    throw new TypeError('Console expects a writable stream instance');
    ^

TypeError: Console expects a writable stream instance
    at new Console (console.js:35:11)
    at Object.<anonymous> (/Users/ryzokuken/Code/temp/node/console/21366.js:11:16)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

Let me investigate.

Wait, it doesn't occur on v10.4.1. Is this a case of a missing backport? Let me try master.

Yeah, master doesn't have it either, so I strongly believe that it's just a matter of a missing backport.

@Q-Angelo try using another release?

Oh, stupid me. As I pointed out, Console(opts) doesn't work in Node v8.x, so changing the line which says:

const logger = new Console({ stdout: output, stderr: errorOutput });

to

const logger = new Console(output, errorOutput);

would fix everything. That said, the above code would work perfectly from Node v10.x onwards.

Closing this because it seems like your problem must be fixed, but feel free to reopen if you feel otherwise.

You're right. I looked at the source code and found the problem

v10.x

function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
  if (!(this instanceof Console)) {
    return new Console(...arguments);
  }

  if (!options || typeof options.write === 'function') {
    options = {
      stdout: options,
      stderr: arguments[1],
      ignoreErrors: arguments[2]
    };
  }

  const {
    stdout,
    stderr = stdout,
    ignoreErrors = true,
    colorMode = 'auto'
  } = options;
}

v8.5

function Console(stdout, stderr, ignoreErrors = true) {
  ...
}

Now that the problem is solved, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danialkhansari picture danialkhansari  Â·  3Comments

dfahlander picture dfahlander  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

cong88 picture cong88  Â·  3Comments

ksushilmaurya picture ksushilmaurya  Â·  3Comments