Search code examples
node.jsspawnpty

Pipe Node stdout in realtime


I have a spawn process running a docker pull and I'm using the following:

const proc = spawn('docker', [ 'pull', 'some/container' ], { env: process.env, cwd: process.env.HOME })
proc.stdout.pipe(process.stdout)

As it runs it breaks up and downloads the individual SHA's and the above works pretty well, however, it puts each response on a new line. I'm curious if there's a way to emulate the "normal" output so each line writes as it pull the image.


Solution

  • If you're just piping to process.stdout, then you could just set the stdio option like:

    const proc = spawn('docker', [
      'pull',
      'some/container'
    ], {
      env: process.env,
      cwd: process.env.HOME,
      stdio: ['pipe', process.stdout, 'pipe']
    });
    

    The end result being that docker will now see its stdout as a TTY (assuming this node script is being run from a terminal/pty of course) and not a pipe.