Node: How to unref readline?

Created on 17 Nov 2020  ยท  5Comments  ยท  Source: nodejs/node

Is your feature request related to a problem? Please describe.

const readline = require('readline');

const input = readline.createInterface({
    input: process.stdin
});

The code above just hangs.

Describe the solution you'd like

const readline = require('readline');

const input = readline.createInterface({
    input: process.stdin
});

+input.unref();

Describe alternatives you've considered

None yet.

doc good first issue readline

Most helpful comment

Solved via process.stdin.unref();

All 5 comments

Solved via process.stdin.unref();

Although I think it would be nice to add this to docs, so I'll reopen.

@szmarczak I think instead of hanging, the program is waiting for the end of stdin.
If you start logging what the program reads like the docs say, you will see that it comes to an end after the input is over:

const readline = require('readline');

const input = readline.createInterface({
    input: process.stdin
});

input.on('line', (line) => {
    console.log(line);
})

Here is the log of a run:

โฏ time echo 'here is a line'|node index
here is a line
echo 'here is a line'  0.00s user 0.00s system 33% cpu 0.002 total
node index  0.07s user 0.02s system 63% cpu 0.140 total

@szmarczak if still need to address this issue and add this functionality to the doc I would like to take this and open a PR.
Just want to make sure what is being asked... in this API doc I need to add an example as you described? can you maybe give me any advice about in which section do you think that it would be wise to add this example to?

At the end of readline.createInterface(options) section

Was this page helpful?
0 / 5 - 0 ratings

Related issues

addaleax picture addaleax  ยท  3Comments

filipesilvaa picture filipesilvaa  ยท  3Comments

loretoparisi picture loretoparisi  ยท  3Comments

seishun picture seishun  ยท  3Comments

mcollina picture mcollina  ยท  3Comments