我正在将一个CLI库从Ruby移植到Node.js。在我的代码中,我会在必要时执行几个第三方二进制文件。我不确定如何在Node中最好地实现这一点。
下面是一个用Ruby语言编写的示例,我调用PrinceXML将文件转换为PDF:
cmd = system("prince -v builds/pdf/book.html -o builds/pdf/book.pdf")
Node中对应的代码是什么?
发布于 2013-12-18 03:59:52
您正在寻找child_process.exec
示例如下:
const exec = require('child_process').exec;
const child = exec('cat *.js bad_file | wc -l',
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
发布于 2017-04-15 22:24:03
const exec = require("child_process").exec
exec("ls", (error, stdout, stderr) => {
//do whatever here
})
发布于 2017-05-25 00:17:02
如果您想要与top answer非常相似但又是同步的东西,那么这是可行的。
var execSync = require('child_process').execSync;
var cmd = "echo 'hello world'";
var options = {
encoding: 'utf8'
};
console.log(execSync(cmd, options));
https://stackoverflow.com/questions/20643470
复制相似问题