Nodejs: How to make a 'system("pause")' or 'Readkey()'?

Asked

Viewed 224 times

0

I’m running a JS code using Sublime Text, cmd automatically closes after executing the JS code, so I don’t see the results so I thought I’d use something similar to the system("pause") function or the getch() function of C/C++ there’s also the Readkey() of C#.

A few basic examples of what I look for in Javascript in other languages:

C#


using System;

class Exe{
    static void Main(){
        // Apesar de Não estar no Padrão .NET o Código funciona Perfeitamente  
        Console.Write("Olá Mundo!\nPressione Qualquer tecla para Encerrar a Execução do Programa...");
        Console.ReadKey();
    }
}

Python 3


print('Olá Mundo!\nPressione Enter para Encerrar a Execução do Programa...', end='')
sysPause = str(input(''))       # Precisa pressionar Enter, diferente dos Outros exemplos...


C


#include <stdio.h>
#include <stdlib.h>

int main(){
    printf("Olá Mundo!\n");
    system("pause");

    return 0;
}

C++


#include <iostream>
#include <conio.h>

using namespace std;
int main(){
    cout << "Olá Mundo!\nPressione Qualquer tecla para Encerrar a Execução do Programa...";
    getch();        // ou getche()

    return 0;
}


1 answer

1

well I don’t know if I’ve interpreted your question correctly but I’ll try to help you.

In pure Javascript it was not thought to receive input from the terminal (alias did not even think to run javascript by terminal);

So you don’t find a native method like in other languages, during the evolution of javascript some modules were created that work things like the flow of input.


Perhaps the most remarkable of them is readline, which is part of the Ode’s own set of internal modules today.

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Qual sua idade? ', (answer) => {
  console.log(`Sua idade é ${answer}`);
  rl.close();
});

Note the above example, fragment taken from and adapted from the readline


If you are using the npm or some other dependency manager, you can opt for simpler options like:

readline-Sync ( that’s even of synchronous )

const readline = require('readline-sync');

// resto do seu codigo.

readline.question('\npress any key!');

I particularly like it very much, because it’s simpler than the readline and not need to chain ''.


Good remembering that there are other modules working on top of the input 'readline', a search in npmjs with the keyword readline reveals interesting libraries.

Search Results


Basically that’s it, I just got a question you say you’re running for the sublime... I don’t know much about the tool.. but depending on how you perform the process is invoked by background (so there is no way you give input)... anyway do a test on the terminal / CMD if you are in doubt...

I hope I helped, hugs.

  • helped same guy worth, in the case of sublime created 2 build for JS, one that only runs the command "Node", "$file_name" and runs the JS code on the sublime console (which does not work to read keyboard data, even in python, java but displays all prints defined, so I see the results, what you said, does not accept user input), and the "start", "Node", "$file_name" that runs the script in Windows cmd, but after code execution cmd is automatically closed by OS.

  • thanks again

Browser other questions tagged

You are not signed in. Login or sign up in order to post.