{"id":15155,"library":"node-run-cmd","title":"node-run-cmd","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/c-h-/node-run-cmd","tags":["javascript","node","console","terminal","bash","command","shell"],"install":[{"cmd":"npm install node-run-cmd","lang":"bash","label":"npm"},{"cmd":"yarn add node-run-cmd","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-run-cmd","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is primarily designed for CommonJS (`require`) usage in Node.js environments. While ESM import might work with specific loader configurations, the documented and intended usage is via `require`. The module exports a single object containing the `run` method.","wrong":"import { run } from 'node-run-cmd';","symbol":"nrc","correct":"const nrc = require('node-run-cmd');"}],"quickstart":{"code":"const nrc = require('node-run-cmd');\n\n// Run multiple commands sequentially, setting a specific working directory for one of them,\n// and handle both successful completion and errors using promises.\nnrc.run([\n  'mkdir my-temp-dir',\n  { command: 'echo \"Hello, world!\" > message.txt', cwd: 'my-temp-dir' },\n  'ls my-temp-dir'\n]).then(function(exitCodes) {\n  console.log('All commands finished successfully with exit codes:', exitCodes);\n  // Clean up\n  return nrc.run('rm -rf my-temp-dir');\n}).then(() => {\n  console.log('Cleanup successful.');\n}).catch(function(err) {\n  console.error('One or more commands failed:', err);\n  // Attempt cleanup even if a command failed\n  nrc.run('rm -rf my-temp-dir').catch(cleanupErr => console.error('Cleanup also failed:', cleanupErr));\n});","lang":"javascript","description":"Demonstrates installing the package, running multiple shell commands with options, and handling promise-based success and error states, including a basic cleanup."},"warnings":[{"fix":"If cross-platform compatibility is a requirement, consider using platform-agnostic Node.js APIs (e.g., `fs` module) or libraries like `node-fs-extra` for file system operations, or abstracting commands based on `process.platform`.","message":"The package explicitly warns against using it for applications requiring cross-platform compatibility, as shell commands are inherently platform-specific.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For common file system operations (e.g., creating/deleting directories or files), prefer using `node-fs-extra` to leverage its native Node.js APIs and cross-platform reliability.","message":"It is not recommended for filesystem-related tasks if `node-fs-extra` can achieve the same result, as `node-fs-extra` provides a more robust and cross-platform compatible API.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be meticulous about command string construction, especially with paths and arguments containing spaces or special characters. Use proper escaping (e.g., backticks, double quotes) according to the target shell. For robustness, consider passing command arguments as an array if the underlying `child_process` API allows.","message":"Incorrect handling of shell-specific syntax, quoting, or command paths can lead to unexpected behavior or failures.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Verify that the command (`<command>`) is installed and available in the system's PATH. If not, provide the full absolute path to the executable or ensure it's installed globally or within `node_modules/.bin` if applicable.","cause":"The command specified does not exist in the system's PATH, or the provided path to the executable is incorrect.","error":"Error: spawn <command> ENOENT"},{"fix":"Inspect the `stderr` output (available via the `onError` callback or within the error object of the promise rejection) to diagnose the specific issue with the command. This often relates to incorrect arguments, file permissions, or environmental issues for the command itself.","cause":"The executed shell command returned a non-zero exit code, indicating an error during its execution.","error":"Command failed to run with error: Error: Command failed: <command> ..."},{"fix":"Ensure that `const nrc = require('node-run-cmd');` is correctly placed and executed before attempting to call `nrc.run(...)`. Verify the package is installed in `node_modules`.","cause":"This error occurs when `nrc` is undefined or does not have a `run` method, typically due to an incorrect or missing `require('node-run-cmd')` statement.","error":"TypeError: nrc.run is not a function"}],"ecosystem":"npm"}