This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

ShellJS Cheatsheet

A quick reference guide for ShellJS: portable Unix shell commands for Node.js scripts across Windows, macOS, and Linux.

· Published

ShellJS is a portable (Windows/Linux/macOS) implementation of Unix shell commands on top of the Node.js runtime.

Basic Commands

const shell = require('shelljs');

// Check binary availability
if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');
  shell.exit(1);
}

// Copy files
shell.cp('-R', 'source/', 'dest/');

// Remove files and directories
shell.rm('-rf', 'build/');

// Create directories recursively
shell.mkdir('-p', 'dist/assets/css');

Navigation & Executing Shell Binaries

// Navigation
shell.cd('dist');
console.log('Current directory:', shell.pwd().toString());

// Execute command synchronously
const result = shell.exec('git rev-parse HEAD');
if (result.code !== 0) {
  shell.echo('Git command failed');
} else {
  shell.echo(`Commit hash: ${result.stdout.trim()}`);
}

File Inspections & Text Processing

// Search text matching regex in files (like grep)
const matches = shell.grep('TODO', 'src/*.js');

// Pipe commands together
shell.cat('input.txt').grep('ERROR').to('errors.log');

References

Related topics