我试图在节点中重复执行 shelljs exec 类型

Im trying to reduplicate the shelljs exec type execution in node

我之前使用的是 shelljs,我以前是这样调用命令的:

require('shelljs/global');

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

但我想在不依赖shelljs的情况下在节点中实现这一点。 事情是我已经找到了节点的例子,但我遇到了很多问题。 感谢帮助

最简单的答案是使用 synchronous version of exec。 stdout 不会在命令运行时出现,结果不会有 shelljs 提供的漂亮属性,只有 stdout data

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

完整的 shelljs 实现在 exec.js, which runs the command via exec-child.js 脚本中,以启用同步执行的实时输出 + 捕获到变量。解决方案的复杂程度取决于您需要的功能。