1

I was using shelljs before where i used to call a command like so:

require('shelljs/global');

let res = exec('echo hello').stdout

But I would like to achieve this in node without relying on shelljs. Thing is I've found examples with node and I'm having trouble with quite a few of them. Thanks for the help

4

1 回答 1

1

超简单的答案是使用exec的同步版本。标准输出不会在命令运行时出现,结果不会有 shelljs 提供的很好的属性,只有数据stdout

const { execSync } = require('child_process')
const exec = (cmd) => execSync(cmd).toString()
const res = exec('echo "hello there"')

完整的 shelljs 实现在exec.js中,它通过exec-child.js脚本运行命令以启用实时输出 + 捕获到同步 exec 的变量。解决方案的复杂程度取决于您需要的功能。

于 2020-10-01T04:06:31.940 回答