Cannot trigger 'end' event using CTRL D when reading from stdin












10















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: ♦♦









share|improve this question



























    10















    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: ♦♦









    share|improve this question

























      10












      10








      10


      2






      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: ♦♦









      share|improve this question














      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: ♦♦






      node.js events stdin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 6 '13 at 15:14









      MisakiMisaki

      59711128




      59711128
























          3 Answers
          3






          active

          oldest

          votes


















          5














          I'd change this:



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


          To this:



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


          Further resources: process docs






          share|improve this answer
























          • but why?....i get this code from the node.js site. Is it incorrect?

            – Misaki
            May 6 '13 at 17:45











          • What version of Node.js are you using?

            – thtsigma
            May 6 '13 at 17:50











          • i'm using 0.10.4

            – Misaki
            May 6 '13 at 17:57











          • 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 6 '13 at 18:32



















          7














          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.






          share|improve this answer































            0















            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.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16401778%2fcannot-trigger-end-event-using-ctrl-d-when-reading-from-stdin%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              I'd change this:



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


              To this:



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


              Further resources: process docs






              share|improve this answer
























              • but why?....i get this code from the node.js site. Is it incorrect?

                – Misaki
                May 6 '13 at 17:45











              • What version of Node.js are you using?

                – thtsigma
                May 6 '13 at 17:50











              • i'm using 0.10.4

                – Misaki
                May 6 '13 at 17:57











              • 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 6 '13 at 18:32
















              5














              I'd change this:



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


              To this:



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


              Further resources: process docs






              share|improve this answer
























              • but why?....i get this code from the node.js site. Is it incorrect?

                – Misaki
                May 6 '13 at 17:45











              • What version of Node.js are you using?

                – thtsigma
                May 6 '13 at 17:50











              • i'm using 0.10.4

                – Misaki
                May 6 '13 at 17:57











              • 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 6 '13 at 18:32














              5












              5








              5







              I'd change this:



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


              To this:



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


              Further resources: process docs






              share|improve this answer













              I'd change this:



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


              To this:



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


              Further resources: process docs







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 6 '13 at 17:29









              thtsigmathtsigma

              3,04412028




              3,04412028













              • but why?....i get this code from the node.js site. Is it incorrect?

                – Misaki
                May 6 '13 at 17:45











              • What version of Node.js are you using?

                – thtsigma
                May 6 '13 at 17:50











              • i'm using 0.10.4

                – Misaki
                May 6 '13 at 17:57











              • 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 6 '13 at 18:32



















              • but why?....i get this code from the node.js site. Is it incorrect?

                – Misaki
                May 6 '13 at 17:45











              • What version of Node.js are you using?

                – thtsigma
                May 6 '13 at 17:50











              • i'm using 0.10.4

                – Misaki
                May 6 '13 at 17:57











              • 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 6 '13 at 18:32

















              but why?....i get this code from the node.js site. Is it incorrect?

              – Misaki
              May 6 '13 at 17:45





              but why?....i get this code from the node.js site. Is it incorrect?

              – Misaki
              May 6 '13 at 17:45













              What version of Node.js are you using?

              – thtsigma
              May 6 '13 at 17:50





              What version of Node.js are you using?

              – thtsigma
              May 6 '13 at 17:50













              i'm using 0.10.4

              – Misaki
              May 6 '13 at 17:57





              i'm using 0.10.4

              – Misaki
              May 6 '13 at 17:57













              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 6 '13 at 18:32





              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 6 '13 at 18:32













              7














              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.






              share|improve this answer




























                7














                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.






                share|improve this answer


























                  7












                  7








                  7







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 10 '15 at 6:21









                  MarkMark

                  7516




                  7516























                      0















                      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.






                      share|improve this answer




























                        0















                        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.






                        share|improve this answer


























                          0












                          0








                          0








                          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.






                          share|improve this answer














                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 7:29









                          Aavgeen singhAavgeen singh

                          111110




                          111110






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16401778%2fcannot-trigger-end-event-using-ctrl-d-when-reading-from-stdin%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith