node-run-cmd

1.0.1 · active · verified Tue Apr 21

node-run-cmd is a Node.js package designed to execute console or terminal commands programmatically. It supports both single and multiple commands, with options for sequential or parallel execution. The current stable version is 1.0.1, marking its initial public release. It offers robust control over command execution, including setting working directories, environment variables, detached mode, and callbacks for data, errors, and completion. It returns promises for asynchronous handling and also supports a callback style. Key differentiators include its detailed options object for fine-grained control over child processes and the ability to define different working directories for individual commands within an array. The package explicitly cautions against its use for cross-platform applications or when `node-fs-extra` can accomplish filesystem-related tasks more effectively, positioning itself as a solution for specific, platform-dependent command execution needs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates installing the package, running multiple shell commands with options, and handling promise-based success and error states, including a basic cleanup.

const nrc = require('node-run-cmd');

// Run multiple commands sequentially, setting a specific working directory for one of them,
// and handle both successful completion and errors using promises.
nrc.run([
  'mkdir my-temp-dir',
  { command: 'echo "Hello, world!" > message.txt', cwd: 'my-temp-dir' },
  'ls my-temp-dir'
]).then(function(exitCodes) {
  console.log('All commands finished successfully with exit codes:', exitCodes);
  // Clean up
  return nrc.run('rm -rf my-temp-dir');
}).then(() => {
  console.log('Cleanup successful.');
}).catch(function(err) {
  console.error('One or more commands failed:', err);
  // Attempt cleanup even if a command failed
  nrc.run('rm -rf my-temp-dir').catch(cleanupErr => console.error('Cleanup also failed:', cleanupErr));
});

view raw JSON →