What is shelljs.exec?
The shelljs.exec package is part of the ShellJS library, which provides portable Unix shell commands for Node.js. It allows you to execute shell commands synchronously within a Node.js environment, making it easier to automate tasks and scripts that require command-line operations.
What are shelljs.exec's main functionalities?
Execute Shell Commands
This feature allows you to execute shell commands directly from your Node.js script. The `exec` function runs the specified command and returns an object containing the output, exit code, and error message if any. In this example, the command `echo hello world` is executed, and the output is logged to the console.
require('shelljs/global');
var result = exec('echo hello world');
console.log(result.stdout);
Capture Command Output
You can capture the output of a shell command by using the `exec` function with the `silent` option set to true. This suppresses the command's output to the console and allows you to handle it programmatically. In this example, the `ls` command is executed, and its output is captured and logged.
require('shelljs/global');
var result = exec('ls', {silent:true});
console.log(result.stdout);
Error Handling
The `exec` function provides error handling by returning an object with an `code` property that indicates the exit code of the command. If the command fails, you can access the error message through the `stderr` property. This example demonstrates how to handle a failed command execution.
require('shelljs/global');
var result = exec('invalidcommand');
if (result.code !== 0) {
console.error('Command failed:', result.stderr);
}
Other packages similar to shelljs.exec
child_process
The `child_process` module is a built-in Node.js module that provides the ability to spawn child processes and execute shell commands. It offers more flexibility and control over process execution compared to shelljs.exec, but requires more setup and handling of streams and events.
execa
Execa is a modern alternative to the built-in `child_process` module, providing a more user-friendly API for executing shell commands. It offers promise-based execution, better error handling, and more features like streaming output. Compared to shelljs.exec, Execa is more feature-rich and suitable for complex use cases.
cross-spawn
Cross-spawn is a package that provides a consistent API for spawning child processes across different platforms. It addresses issues with the built-in `child_process` module on Windows. While shelljs.exec focuses on synchronous command execution, cross-spawn is more about ensuring compatibility and reliability across environments.
shelljs.exec

Replacement for shelljs' slow exec method - benchmarked 20x faster
Introduction
shelljs is FAST but its exec method is SLOW
This module offers a shelljs.exec like interface and is a zero-dependency module
Usage
This module takes 2 arguments, the command to execute and an options object
var exec = require('shelljs.exec')
var cmdObj = exec('echo hello', {silent: true})
Continuing from the code above, the resulting JSON looks like:
expect(cmdObj).to.eql({
error: null,
stdout: 'hello\n',
stderr: '',
code: 0,
ok: true
})
Options object
Available options:
-
silent
: Do not echo any output to console, defaults to false
-
and any option available to Node.js' child_process.execSync
When the stdio
option of child_process.execSync is provided, it overrides silent
Benchmarks
Logs for benchmarks are here and specs are here
Generally speaking, on Linux shelljs.exec (this module) is 20x faster and on Windows 5x faster than shelljs' exec method.
However, shelljs supported commands, where available, are much faster than both. Use this module where no equivalent shelljs supported command is available.
These tables compare average command execution times based on 1000 test runs:
Linux (Travis CI) Node v6

Windows (GIT bash) Node v6.10.0

Limitations
Author says
But as the days of Noah were, so also will the coming of the Son of Man be. For as in the days before the flood, they were eating and drinking, marrying and giving in marriage, until the day that Noah entered the ark, and did not know until the flood came and took them all away, so also will the coming of the Son of Man be.
Matthew 24:37-39