Ben Borgers / Posts /

Logging the output of Node.js shell exec

January 24, 2021 by @benborgers

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.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!

Loading...