Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including shx with all npm packages installed. Try it out:

shx lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

// require("shx/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

shx v0.3.2

Portable Shell Commands for Node

Shx

Travis AppVeyor Codecov npm version npm downloads

shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts.

Difference Between ShellJS and shx

  • ShellJS: Good for writing long scripts, all in JS, running via NodeJS (e.g. node myScript.js).
  • shx: Good for writing one-off commands in npm package scripts (e.g. "clean": "shx rm -rf out/").

Install

npm install shx --save-dev

This will allow using shx in your package.json scripts.

Usage

Command Line

If you'd like to use shx on the command line, install it globally with the -g flag. The following code can be run either a Unix or Windows command line:

$ shx pwd                       # ShellJS commands are supported automatically
/home/username/path/to/dir

$ shx ls                        # files are outputted one per line
file.txt
file2.txt

$ shx rm *.txt                  # a cross-platform way to delete files!

$ shx ls

$ shx echo "Hi there!"
Hi there!

$ shx touch helloworld.txt

$ shx cp helloworld.txt foobar.txt

$ shx mkdir sub

$ shx ls
foobar.txt
helloworld.txt
sub

$ shx rm -r sub                 # options work as well

$ shx --silent ls fakeFileName  # silence error output

All commands internally call the ShellJS corresponding function, guaranteeing cross-platform compatibility.

package.json

ShellJS is good for writing long scripts. If you want to write bash-like, platform-independent scripts, we recommend you go with that.

However, shx is ideal for one-liners inside package.json:

{
  "scripts": {
    "clean": "shx rm -rf build dist && shx echo Done"
  }
}

Unsupported Commands

Due to the differences in execution environments between ShellJS and shx (JS vs CLI) some commands are not supported:

Unsupported commandRecommend workaround
shx cdJust use plain old cd (it's the same on windows too)
shx pushdJust use plain old pushd. Use forward slashes and double-quote the path. (e.g. pushd "../docs". This would fail on Windows without the quotes)
shx popdJust use plain old popd
shx dirsNo workaround
shx setSee below
shx exitJust use plain old exit
shx execInstead of shx exec cmd, just use plain old cmd
shx ShellStringNo workaround (but why would you want this?)

Shx options

Shx allows you to modify its behavior by passing arguments. Here's a list of supported options:

set flagshell.config settingshx commandEffect
-econfig.fatal = trueNot supportedExit upon first error
-vconfig.verbose = trueshx --verbose cd fooLog the command as it's run
-fconfig.noglob = trueshx --noglob cat '*.txt'Don't expand wildcards
N/Aconfig.silent = trueshx --silent cd noexistDon't show error output

Team

Nate FischerAri PoradLevi Thomason
Nate FischerAri PoradLevi Thomason
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free