节点子进程执行命令失败,错误代码为1

时间:2016-01-05 22:41:04

标签: node.js cmd typescript visual-studio-code child-process

我正在尝试使用节点js子进程执行某些行并获取错误。 以下是我的代码:

let cmd : string = "code " + PROJECTS[value];
exec(cmd, function callback(error, stdout, stderr) {
  console.log("started console app");
});

错误:

cmd:"C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-syn... (length: 82)"
code:1
killed:false
message:"Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\c... (length: 99)"
signal:null
stack:undefined

错误详情JSON。

Full CMD : "C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync""
Full message : "Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync"\n"

2 个答案:

答案 0 :(得分:1)

尝试一个更简单的例子..

var exec = require('child_process').exec;
var cmd = 'code C:\Program Files';
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout); 
});

这有用吗?

答案 1 :(得分:0)

我不想看到整个错误(太冗长),所以我做了这样的事情:

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
  if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    }
    xmlhttp.open("GET", "gethint.php?q="+str, true);
    xmlhttp.send();
  }
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form action="">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>

由于“等待”,它位于“异步”功能内。 详细信息:https://stackoverflow.com/a/56095793/722796