Ben Borgers

Logging the output of Node.js shell exec

July 1, 2020

The nice thing about the built-in child_process package’s spawn command for executing shell commands with Node.js, is that you can easily get access to the output of that command.

This way, you can see what the terminal command is doing:

const { spawn } = require('child_process')

const command = spawn('git clone', {
  shell: true

command.stdout.on('data', data =>   console.log(data.toString()))
command.stderr.on('data', data => console.error(data.toString()))

Now, the output of your spawn shell command will be streamed to the console in real time.

Subscribe to my newsletter!

A weekly round-up of new blog posts and updates to projects I’m working on.