Node.js Node js-如何按顺序执行两个windows命令并获得输出

Node.js Node js-如何按顺序执行两个windows命令并获得输出,node.js,Node.js,我试图按顺序执行两个windows命令,并得到后一个命令的结果。比如: cd ${directory} sfdx force:source:convert -d outputTmp/ --json 我浏览并尝试了很多第三方库,比如node cmd。但到目前为止我还没有任何运气。如节点cmd示例所示: cmd.get( `cd ${directory} sfdx force:source:convert -d outputTmp/ --json`, function(er

我试图按顺序执行两个windows命令,并得到后一个命令的结果。比如:

cd ${directory}
sfdx force:source:convert -d outputTmp/ --json
我浏览并尝试了很多第三方库,比如node cmd。但到目前为止我还没有任何运气。如节点cmd示例所示:

cmd.get(
    `cd ${directory}
    sfdx force:source:convert -d outputTmp/ --json`,
    function(err, data, stderr) {
这在我的macOS机器上运行得很好。但在Windows上,它往往只执行第一个命令

我是否有办法解决这个问题?即使是一些仅使用cd{directory}+real命令的漫游也非常有用

您可以尝试以下方法:

const exec = require('child_process').exec;

exec(`cd dir 
      sfdx force:source:convert -d outputTmp/ --json`, (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
  console.log(stdout);
});
或者使用不带反勾号的
&

const exec = require('child_process').exec;

exec('cd dir && sfdx force:source:convert -d outputTmp/ --json', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
  console.log(stdout);
});

您是否在widows计算机(cf:sfdx)上安装了第二个命令所需的所有软件包?回调中的stderr是否返回任何内容?@NathanSchwarz sfdx运行时没有任何问题。在回调函数中,错误为null,数据为空。如果我直接执行sfdx命令,它没有问题。谢谢。实际上,一个简单的&&就可以解决我的问题!