System commands for JavaScript
Run system commands in Node.js
Installation
npm i system-commands
JavaScript
const system =
TypeScript
Tutorial
Run any command using system(COMMAND)
. The output is passed into the .then
block, and the error (if any) is passed into the .catch
block.
/** * Runs a system command * * Parameter `command` - The command you want to run, like `ls` or `mkdir new_directory` * * Returns a `Promise` containing the output of the command. * If the command failed, the error is passed into the `.catch` block. */
Run the command ls
:
// async/await console.logawait system'ls' // Handling errors system'ls'.then.catch // Or for a more concise statement... system'ls'.thenconsole.log.catchconsole.error // Output: /* * README.md * lib * node_modules * package-lock.json * package.json * src * tests * tsconfig.json * tslint.json * types */
Make a new directory:
system'mkdir new_directory'.then.catch // Output:// Successfully created directory