node.jsconsole

node:console test is producing TypeError: Console is not a constructor


I already know how to use the global implementation on console, but I would like to use the Node.js Console module. The code below is copied from the tutorial. (https://nodejs.dev/en/api/v19/console/)

import Console from 'node:console'; //when using modules
or
const { Console } = require('node:console'); //when using straight js


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);
// In stdout.log: count 5

but I am getting the following error

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

TypeError: Console is not a constructor

I am using Node.js v19.8.1 I have tried both modules and straight js. I can make the global implementation of console work, but not this. What am I doing wrong? Thank you


Solution

  • Using

    console.log(Console)
    

    I was able to find the available methods

    Object [console] {
      log: [Function: log],
      warn: [Function: warn],
      dir: [Function: dir],
      time: [Function: time],
      timeEnd: [Function: timeEnd],
      timeLog: [Function: timeLog],
      trace: [Function: trace],
      assert: [Function: assert],
      clear: [Function: clear],
      count: [Function: count],
      countReset: [Function: countReset],
      group: [Function: group],
      groupEnd: [Function: groupEnd],
      table: [Function: table],
      debug: [Function: debug],
      info: [Function: info],
      dirxml: [Function: dirxml],
      error: [Function: error],
      groupCollapsed: [Function: groupCollapsed],
      Console: [Function: Console],
      profile: [Function: profile],
      profileEnd: [Function: profileEnd],
      timeStamp: [Function: timeStamp],
      context: [Function: context]
    }
    

    so this works

    // use it like console
    const count = 5;
    Console.log('count: %d', count);
    // In stdout.log: count 5