29

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: ♦♦
1
  • 1
    I'm testing on Mac Catalina, NodeJS 12.16.2 and can confirm CTRL+D is working fine with your code Commented Jul 27, 2020 at 14:20

7 Answers 7

19

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

Sign up to request clarification or add additional context in comments.

2 Comments

What version of Node.js are you using?
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.
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.

1 Comment

What'd you suggest to substitute out?
7

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

Comments

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.

1 Comment

It's not triggering process.stdin.on("end") event. Can you please help me with this?
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.

2 Comments

Beware it may affect previous Idea's shortcut; e.g. Ctrl+D to run Debug.
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
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

Comments

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


1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.