Node has a built-in library called child_process
that allows us to execute shell commands using either the child_process.exec()
function or the child_process.spawn()
function.
In this section, you will learn how to execute shell commands using the exec()
function in Node's child_process
library.
In this example, we will execute the coffee
command with the --help
parameter to get the help text displayed by the CoffeeScript executable:
Load the
child_process
library and grab itsexec()
function:exec = require('child_process').exec
Execute CoffeeScript's
coffee --help
command, which will display the help text for CoffeeScript:exec 'coffee -help', (error, stdout, stderr) ->
The following result will be displayed:
if stdout console.log "Received #{stdout.length} bytes." console.log stdout if stderr console.error stderr