Node.js 尝试在节点中运行管道命令

Node.js 尝试在节点中运行管道命令,node.js,command-line,process,Node.js,Command Line,Process,我正在尝试运行命令git branch | grep\*,如下所示: require('child_process').exec('git branch | grep \*', function(err){ console.log(err); }); 但是我一直得到错误{[error:Command failed:]killed:false,code:1,signal:null} 为什么会发生这种情况?我该怎么做?您是否正在尝试运行grep\*?请记住,您必须在字符串中转义反斜杠。这意味着

我正在尝试运行命令
git branch | grep\*
,如下所示:

require('child_process').exec('git branch | grep \*', function(err){
  console.log(err);
});
但是我一直得到错误
{[error:Command failed:]killed:false,code:1,signal:null}


为什么会发生这种情况?我该怎么做?

您是否正在尝试运行
grep\*
?请记住,您必须在字符串中转义反斜杠。

这意味着grep返回代码为1,而grep什么也得不到

您只需编写一个返回1的简单代码,并在node.js中由exec运行它,就可以得到与上面相同的结果

您可以通过err.code检查状态代码,并在回调函数中报告空结果