Skip to content

CONOUT$ no longer available for Node 22? #54161

Description

@jpage-godaddy

Node.js Version

v22.4.1

NPM Version

v10.8.1

Operating System

Windows 11

Subsystem

fs, process

Description

I am trying to fix some code for compatibility with node 22 that writes directly to the terminal. For unix-like OS's, things work fine due to /dev/tty, but for Windows, the code uses process.binding('fs') hackery to open the conout$ magic file. However, this no-longer works in node 22, which treats conout$ as a regular filename and of course cannot find it.

Is there a way to make something like this work for node 22? Is there finally a cleaner option to writing to the Windows console directly without using stdout?

const openWindowsConsole = () => {
  // vX.X.X
  const nodeVersion = parseInt(process.version.split('.')[0].slice(1), 10);

  // For node 22, we seem to be unable to open CONOUT$
  if (nodeVersion >= 22) {
    // What do we do here?
    throw new Error('This application is incompatible with Windows & node 22');
  }

  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)
  );
}

Minimal Reproduction

Using node 22, on Windows run this code:

const fs = require('fs');
process.binding("fs").open('conout$', fs.constants.O_RDWR | fs.constants.O_EXCL, 0o666);

Output

Uncaught [Error: ENOENT: no such file or directory, open 'C:\Users\jpage\conout$'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\jpage\\conout$'
}

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions