Published on

Node.js Shell Commands Made Easy with ShellJS


If you plan on interacting with CLI installed in your machine, then shelljs is a good option.

It abstracts all the complicated things like interacting with child process and provides a nice helpers using which you can your commands from your Node.js app.

And it has support of most common UNIX commands as well.


npm install shelljs

Sample Code

import shell from 'shelljs'
// delete ./dist directory
shell.rm('-rf', 'dist');

// check if git is installed
if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');

// Remove pdf password
const cmd = 'qpdf --decrypt   --replace-input "./protected.pdf" --password="YOUR_PASSWORD"'

if (shell.exec(cmd).code !== 0) {
  shell.echo('Error: qpdf decrypt failed');

Helper Snippet

import shell from 'shelljs'

async function runShellCmd(cmd) {
  return new Promise((resolve, reject) => {
    shell.exec(cmd, async (code, stdout, stderr) => {
      if (!code) {
        return resolve(stdout);
      return reject(stderr);

runShellCmd(`echo "It Works"`);

And much more, checkout their docs for more details

Note: All commands run synchronously

Happy shell scripts!