我以前使用shelljs调用命令,如下所示:
require('shelljs/global');
let res = exec('echo hello').stdout但我希望在不依赖shelljs的情况下在node中实现这一点。问题是,我已经找到了使用node的示例,并且其中相当多的示例遇到了问题。谢谢你的帮助
发布于 2020-10-01 12:06:32
最简单的答案是使用exec的synchronous版本。当命令运行时,标准输出不会出现,结果也不会有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的实时输出+对变量的捕获。这取决于您需要的功能,以及解决方案变得有多复杂。
https://stackoverflow.com/questions/64147686
复制相似问题