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 https://github.com/benborgers/potion', {
  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.

More blog posts

Subscribe to my newsletter for a monthly round-up of new blog posts and projects Iā€™m working on!

Twitter ↗ RSS feed ↗