In this short article, we’ll learn how to execute the shell script using Node.js.
shelljs Module to Execute Shell Script in Node.js
On top of Node.js API, the ShellJS is a portable implementation of Unix shell commands. You could use it to remove your shell script dependency on Unix while keeping your instructions acquainted and powerful.
You can additionally install it globally to run it from outside Node tasks. You can discover more information about the ShellJS documentation for
exec(command [, options] [, callback])
The following are different options for Node.js
async: async means
Asynchronousexecution. It’ll be set to
trueif the callback is provided regardless of the value passed. The default value is
silent: Don’t write program output to console. The default value is
encoding: Encoding of the characters to be used. It affects the values returned to stdout and stderr and what is written to
stderrwhen not in silent mode. The default value is
We will focus on how to execute a shell script in Node.js using the
First, install the
shelljs library using the following command.
$ npm i shelljs
The command below will check the version of
const shell = require('shelljs') shell.exec("npm --version")
Instead of a command, you can also specify the path of your shell file.