I am trying to fix a node.js package that writes directly to the console (/dev/tty
in Unix-likes) in the case of Windows. It’s using some internal bindings, sadly, which means it breaks with different Node.js versions, but I am able to get it working with node 18 and 20:
// vX.X.X
const nodeVersion = parseInt(process.version.split('.')[0].slice(1), 10)
const openArgs = [
"CONOUT$",
// eslint-disable-next-line no-bitwise
fs.constants.O_RDWR | fs.constants.O_EXCL,
0o666
];
if (nodeVersion < 20) {
// Older version of node require null being passed explicitly for the
// optional arguments, but this breaks node 20.
openArgs.push(null, null)
}
return new tty.WriteStream(
process.binding("fs").open(...openArgs)
);
However, in node 22, it appears that CONOUT$
is not available for opening:
Error: ENOENT: no such file or directory, open 'C:myprojectCONOUT$'
Does anyone know how opening “special” files like this on Node 22 can be accomplished? Better yet, does anyone have a less hacky way to write directly to the console? The original author does not want to just use stdout
due to them wanting to reserve it for piping output.