Skip to content

How to open exist tty using net.Socket ? #37780

Description

@oppai

Hi, there

I want to open exist tty using net.Socket. But I got an error like a below.

I have two questions.
First, how can I open tty using net.Socket module ?
Second, I success to open tty using tty.ReadStream like a them. But I didn't find it in document. It is trial function? (It is not safe to use in production?)

thank you

  • Node.js Version: v14.16.0
  • OS: MacOS / Linux
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): net.Socket
var fs = require('fs')
var net = require('net')

const fd = fs.openSync('/dev/ttys003', 'r+');
const tty = new net.Socket({fd, readable: true})
tty.on('data', (data) => {
  console.log(data)
})
TypeError [ERR_INVALID_FD_TYPE]: Unsupported fd type: TTY
    at createHandle (net.js:147:9)
    at new Socket (net.js:328:22)
    at Object.<anonymous> (/Users/hiroaki.murayama/progate/progate_ide/v2/src/runtime/sample.js:5:13)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_FD_TYPE'

Activity

  1. RaisinTen commented on Mar 16, 2021

    @RaisinTen
    Member

    I don't think opening a socket to a TTY fd is supported yet.
    At the bottom of https://nodejs.org/api/net.html#net_class_net_socket:

    The newly created socket can be either a TCP socket or a streaming IPC endpoint, depending on what it connect() to.

  2. addaleax commented on Mar 16, 2021

    @addaleax
    Member

    @RaisinTen No, it is supported. I think this might also qualify as a bug in the net module.

    In any case, one can use tty.ReadStream(fd) and tty.WriteStream(fd) to create TTY streams (which should be one class but for some reason isn’t), and both of these inherit from net.Socket.

  3. RaisinTen commented on Mar 16, 2021

    @RaisinTen
    Member

    @addaleax The passed fd type gets rejected here:

    node/lib/net.js

    Lines 140 to 156 in 3bba40a

    function createHandle(fd, is_server) {
    validateInt32(fd, 'fd', 0);
    const type = guessHandleType(fd);
    if (type === 'PIPE') {
    return new Pipe(
    is_server ? PipeConstants.SERVER : PipeConstants.SOCKET
    );
    }
    if (type === 'TCP') {
    return new TCP(
    is_server ? TCPConstants.SERVER : TCPConstants.SOCKET
    );
    }
    throw new ERR_INVALID_FD_TYPE(type);
    }
    . So, this can be fixed by adding an option to accept TTY fds. Should we move this issue to the main repo?

  4. addaleax commented on Mar 16, 2021

    @addaleax
    Member

    Yeah, although I think there might also be another issue somewhere for merging the two TTY classes? I think that would be the best solution here.

  5. oppai commented on Mar 17, 2021

    @oppai
    Author

    Thank you, @addaleax and @RaisinTen !
    I understand that net.Socket is not supported for tty fd, and I should use tty module instead of net.Socket.

    So, there is no document about accepting fd in tty modules.
    https://nodejs.org/api/tty.html#tty_class_tty_readstream

    it is missing? or experimental function?

  6. addaleax commented on Mar 17, 2021

    @addaleax
    Member

    I don’t know why the constructors are not documented, but they should be, yes.

  7. transferred this issue fromnodejs/helpon Mar 17, 2021
  8. added
    docIssues and PRs related to Node.js documentation.
    ttyIssues and PRs related to the tty subsystem.
    on Mar 31, 2021
  9. added a commit that references this issue on Apr 21, 2025
  10. added a commit that references this issue on Jun 9, 2025
  11. added a commit that references this issue on Jul 2, 2025
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

    docIssues and PRs related to Node.js documentation.ttyIssues and PRs related to the tty subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions