28

In the following code

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function(chunk) {
  process.stdout.write('data: ' + chunk);
});

process.stdin.on('end', function() {
  process.stdout.write('end');
});

i can't trigger the 'end' event using ctrl+D, and ctrl+C just exit without triggering it.

hello
data: hello
data
data: data
foo
data: foo
^F
data: ♠
^N
data: ♫
^D
data: ♦
^D^D
data: ♦♦
Fadwa
  • 1,717
  • 5
  • 26
  • 43

7 Answers7

18

I'd change this (key combo Ctrl+D):

process.stdin.on('end', function() {
    process.stdout.write('end');
});

To this (key combo Ctrl+C):

process.on('SIGINT', function(){
    process.stdout.write('\n end \n');
    process.exit();
});

Further resources: process docs

Chong Onn Keat
  • 520
  • 2
  • 8
  • 19
thtsigma
  • 4,878
  • 2
  • 28
  • 29
  • What version of Node.js are you using? – thtsigma May 06 '13 at 17:50
  • I'm not sure on the Why part. I looked around a bit and didn't see any obvious suggestions. My guess is that stdin.on is not receiving an end event, thus it never calls that. But again, its just a guess. – thtsigma May 06 '13 at 18:32
12

I too came upon this problem and found the answer here: Github issue

The readline interface that is provided by windows itself (e.g. the one that you are using now) does not support ^D. If you want more unix-y behaviour, use the readline built-in module and set stdin to raw mode. This will make node interpret raw keypresses and ^D will work. See http://nodejs.org/api/readline.html.

If you are on Windows, the readline interface does not support ^D by default. You will need to change that per the linked instructions.

Mark
  • 606
  • 7
  • 12
6

Alternatively

  1. Use an input file containing your test data for e.g. input.txt
  2. Pipe your input.txt to node

cat input.txt | node main.js

Avinash
  • 61
  • 1
  • 2
5

If you are doing it in context to Hackerrank codepair tool then this is for you.

The way tool works is that you have to enter some input in the Stdin section and then click on Run which will take you to stdout.

All the lines of input entered in the stdin will be processed by the process.stdin.on("data",function(){}) part of the code and as soon as the input "ends" it will go straight to the process.stdin.on("end", function(){}) part where we can do the processing and use process.stdout.write("") to output the result on the Stdout.

process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    // This is where we should take the inputs and make them ready.
    input += (chunk+"\n");
    // This function will stop running as soon as we are done with the input in the Stdin
});
process.stdin.on("end", function () {
    // When we reach here, we are done with inputting things according to our wish.
    // Now, we can do the processing on the input and create a result.
    process.stdout.write(input);
});

You can check the flow by pasting he above code on the code window.

Aavgeen singh
  • 435
  • 4
  • 12
1

I've faced this too while debugging the code from Hackerrank using IntelliJ IDEA on Mac. Need to say that without IDEA, executing exactly the same command in terminal - everything worked fine.

At first, I've found this: IntelliJ IDEA: send EOF symbol to Java application - and surprisingly, Cmd+D works fine and sends EOF.

And then, diving into IDEA settings, I've found "Other -> Send EOF", which was Cmd+D by default. After adding the second shortcut to this (Ctrl+D) - everything works as I've used to.

cub-uanic
  • 141
  • 2
  • 8
  • Beware it may affect previous Idea's shortcut; e.g. `Ctrl+D` to run `Debug`. – Ricardo Mar 16 '21 at 05:01
  • Also for those testing Hackerrank on Idea / WebStorm, you may want to `Edit Configuration...` and add the environment variable, such as `OUTPUT_PATH=./output.txt` – Ricardo Mar 16 '21 at 05:03
1

You can use redirectionoperator to feed the input from a file

$ node main.js < input.txt


[ only works in Unix-based machine like MacBook, Linux... ]

Alternatively, after typing the input into shell, you can press Ctrl + D to send EOF(end-of-file) to trigger the event handler in process.stdin.on("end", ...)

Ctrl+D(^D) is not supported in Microsoft Windows by default as mentioned by @Mark

Chong Onn Keat
  • 520
  • 2
  • 8
  • 19
1

you can also try something like this if you want to run programs using node on your windows pc, here i have triggered end using ctrl+d, hope it helps


'use strict';
const { METHODS } = require('http');
const readline = require('readline')
process.stdin.resume();
process.stdin.setEncoding('utf-8');
readline.emitKeypressEvents(process.stdin);
let inputString = '';
let currentLine = 0;
process.stdin.setRawMode(false)
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('keypress', (str, key) => {
    if (key && key.ctrl && key.name == 'd'){
        inputString = inputString.trim().split('\n').map(string => {
            return string.trim();
        })
        main();  
    }
});
function readLine() {
    return inputString[currentLine++];
}
function method() {

}
function main() {
    const n = parseInt(readLine().trim());
    const arr = readLine().replace(/\s+$/g, '').split(' ').map(qTemp =>parseInt(qTemp,10))
    method();
}


tarak
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 11:23