Ben Borgers

⇢ Twitter

Logging the output of Node.js shell exec

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.


Last updated January 24, 2021