how to get textual stdout from console.log(require("child_process").execSync('ls'))?
Asked Answered
P

1

5

How to print the stdout from console.log(require("child_process").execSync('ls'))

I tried in ts

import { execSync } from 'child_process';
console.log(execSync('ls -la'));

which was then compiled to js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
console.log(child_process_1.execSync('ls -la'));

but when I run it I get only like buffer how to get the stdout?

$ node app.js
$ <Buffer 74 6f 74 61 6c 20 38 38 0a 64 72 77 78 72 2d 78 72 2d 78 20 20 31 31 20 74 6f 6d 65 72 2e 62 65 6e 64 61 76 69 64 20 20 73 74 61 66 66 20 20 20 20 33 ... >

What am i missing? how to get the textual stdout?

Propose answered 29/9, 2017 at 5:40 Comment(0)
B
12

Your last line should be:

console.log(child_process_1.execSync('ls -la').toString());

execSync returns a buffer, simply call toString on the buffer to get the contents of the buffer as a string.

Bustard answered 29/9, 2017 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.