Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Node script to run external command requiring user input

I'm trying to write a node script that is able to run an external command that has two layers of user input.

const conf = spawn('my command', {shell: true});


conf.stdout.on('data', (data) => {
    process.stdout.write(data)
    conf.stdin.write("\n")
})

Basically spawn runs and I get the external command's output correctly showing by writing it to stdout, I then simulate the user pressing enter by writing to stdin conf.stdin.write("\n"), the problem I am having is that when that is simulated I get another prompt from the command I ran in spawn and it just goes on a eternal loop.

Is there a way of listening to the first set of data, write the input, read the second set of data and pass some input the user writes in the terminal?

Many thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use the prompt-sync library, get user input and then write it to stdin:

const prompt = require("prompt-sync")();
const { spawn } = require("node:child_process");
const conf = spawn("dir", { shell: true });

conf.stdout.on("data", async (data) => {
  process.stdout.write(data);
  conf.stdin.write("\n");

  const input = prompt("");
  conf.stdin.write(input);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda