Billions McMillions
Billions McMillions

Reputation: 395

Node.js: Writing a function to return spawn stdout as a string

I'm trying to return the output of this function as a string, but it keeps returning as undefined. Where am I going wrong?

function run(cmd){
    var spawn = require('child_process').spawn;
    var command = spawn(cmd);
    var result = '';
    command.stdout.on('data', function(data) {
            result += data.toString();
    });
    command.on('close', function(code) {
            return result;
    });
}
console.log(run('ls'));

Upvotes: 37

Views: 59073

Answers (5)

Joseph Merdrignac
Joseph Merdrignac

Reputation: 3860

Typescript version:
(following common current async usage)

import { spawn } from 'child_process'

const run = (commandLine: string) => new Promise<string>((resolve, reject) => {
  const [command, ...args] = commandLine.split(/\s+/)
  const child = spawn(command, args)
  const output = [] as string[]
  child.stdout.on('data', chunk => output.push(chunk))
  child.on('close', () => resolve(output.join('').trim()))
  child.on('error', error => reject(error))
})

usage:

const branch = await run('git branch --show-current')

Upvotes: 0

Mohammad Yaser Ahmadi
Mohammad Yaser Ahmadi

Reputation: 5051

clean way is using async/await so try like this:

const spawn = require('child_process').spawnSync;
 
try {
    const child = spawn(cmd)
    return { stdout: child.stdout.toString(), stderr: child.stderr.toString() }
} catch (error) {
    console.log(error);
    return error
}

Upvotes: 0

Nelson Owalo
Nelson Owalo

Reputation: 2414

You can always wrap your function in a promise and return that. I find more efficient than @fuwaneko's callback solution

function run(cmd) {
    return new Promise((resolve, reject) => {
        var spawn = require('child_process').spawn;
        var command = spawn(cmd)
        var result = ''
        command.stdout.on('data', function(data) {
             result += data.toString()
        })
        command.on('close', function(code) {
            resolve(result)
        })
        command.on('error', function(err) { reject(err) })
    })
}

Upvotes: 5

fuwaneko
fuwaneko

Reputation: 1165

Your function returns immediately after command.on statement. The return statement in your callback for the close event is returned to nowhere. The return belongs to event callback, not to run().

Put console.log call instead of return result.

Generally speaking you should write something like:

function run(cmd, callback) {
    var spawn = require('child_process').spawn;
    var command = spawn(cmd);
    var result = '';
    command.stdout.on('data', function(data) {
         result += data.toString();
    });
    command.on('close', function(code) {
        return callback(result);
    });
}

run("ls", function(result) { console.log(result) });

Upvotes: 52

Marcel
Marcel

Reputation: 1286

var spawn = require('child_process').spawn,
    command  = spawn('ls', ['/tmp/']);
command.stdout.pipe(process.stdout);

The following link is exactly the same question as yours.

Upvotes: 5

Related Questions