The idea is to execute a command and capture the response in a variable so that it can be processed.
const { stdout } = require('process');
function files(command, arg) {
const { spawn } = require('child_process');
const ls = spawn(command, [arg, '/home']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
return data;
});
}
var ej = files("dir", "")
console.log("eje => " + ej);
When executing the following line you get:
console.log(`stdout: ${data}`); stdout: /home/test/Descargas: a.jpg link.txt
When trying to return the value appears undefined
return data; console.log("eje => " + ej); eje=> undefined