如何从console.log(require("child_process").execSync('ls'))中获得文本标准输出?

5
如何打印console.log(require("child_process").execSync('ls'))的标准输出?我在ts中尝试过。
import { execSync } from 'child_process';
console.log(execSync('ls -la'));

然后将其编译为JS:

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

但是当我运行它时,我只得到了像缓冲区一样的输出,如何获取标准输出?
$ 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 ... >

我错过了什么?如何获取文本标准输出?
1个回答

12

你的最后一行应该是:

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

execSync 返回一个缓冲区,仅需在缓冲区上调用 toString 以将缓冲区内容作为字符串获取。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接