Spawn an async process and get back stdout, handling errors
smart-spawn
Spawn an async process and get back stdout, handling errors
npm install smart-spawn
You need Node 6+. It’ll probably run on 0.10, 0.12 and 4 too, but the test suite doesn’t cover those versions, so you’re on your own. And you should upgrade anyway because those are horribly insecure.
var smartSpawn = require('smart-spawn');
var process = smartSpawn('ls', ['-l'], process.cwd(), function(err, stdout) {
if (err) throw err;
console.log(stdout);
}
Arguments are, in order: the name of the process to spawn, arguments passed to the subprocess, the subprocess’ working directory, and a callback function.
The callback receives two arguments. The first is an Error
object which is passed if the process couldn’t be spawned or if it exited with a nonzero exit code, otherwise it’s undefined
. The second is the process’ stdout upon its completion (regardless of any errors that may have occured).
smart-spawn
will return an instance of ChildProcess
representing the spawned subprocess.
LGPL 3.0+
AJ Jordan alex@strugee.net