I have this script that properly run synchronously the ls
command and output the result to the terminal. How can I intercept the result and save it to a variable?
const cp = require('child_process');
const result = cp.spawnSync(
'ls',
['-l', '/usr'],
{ stdio: [process.stdin, process.stdout, process.stdout] }
);
If I try this, as suggested by https://stackoverflow.com/a/30617874/693271
result.stdout.on('data', function (chunk) {
console.log(chunk);
});
I get
result.stdout.on('data', function (chunk) {
^
TypeError: Cannot read property 'on' of null
The difference is that is about spawnSync
and not about spawn