Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!