Javascript nodejs exec命令失败,没有有用的错误消息

Javascript nodejs exec命令失败,没有有用的错误消息,javascript,node.js,exec,child-process,Javascript,Node.js,Exec,Child Process,这是要执行的代码 cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) { if (e) { var errorstr = "Compilation failed with the following error"+ e.message.toString() client.send(errorstr) co

这是要执行的代码



    cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) {
        if (e) {
            var errorstr = "Compilation failed with the following error
"+ e.message.toString() client.send(errorstr) console.log(e, stdout, stderr) ee.prototype.removeAllListeners() } else if (stderr.length > 0) { client.send("Compilion finished with warnings\n"+ stderr + '\n') client.send('compiled') ee.prototype.emit('compiled') } else { client.send("Compilation successful") ee.prototype.emit('compiled') } })

cp.exec(“cc-Wall/tmp/test.c-o/tmp/test”,函数(e、stdout、stderr){
如果(e){
var errorstr=“编译失败,出现以下错误
”+e.message.toString() client.send(errorstr) console.log(e、stdout、stderr) ee.prototype.removeAllListeners() }否则如果(标准长度>0){ client.send(“编译已完成,但出现警告\n”+stderr+'\n') client.send('已编译') ee.prototype.emit('compiled') }否则{ client.send(“编译成功”) ee.prototype.emit('compiled') } })
“client”是socket.io回调参数的参数ee'是EventEmitter的一个实例

谈到这个问题。在运行代码时,回调表示命令未成功。日志(e、stdout、stderr)是

{[Error:Command failed:]killed:false,code:false,signal:undefined}''
/tmp/test.c是一个有效的c代码,在检查目录/tmp时,我发现test.c是正确的,并且正在生成二进制“test”,并且在shell中运行时正确执行。所以我不明白为什么它会标记不成功的执行。错误对象的信息也没有帮助。如果您能提供一些帮助/解释,我将不胜感激。我有点担心控制台中的输出

{ [Error: Command failed: ] killed: false, code: false, signal: undefined }
看起来不像一个合适的JSON/JavaScript对象,尤其是
[错误:命令失败:]
部分;至少缺少一个逗号

建议:

  • 从命令行运行命令并检查退出代码(使用
    echo$?
    )。如果退出代码为!=0,则这意味着命令“失败”(不管这意味着什么)

  • 当命令失败时,输入
    e.code
    (我在您的输出中缺少了它…)。找出这个值发生了什么

  • 如果(e!==null)尝试
    if(e)
    ,而不是
    if(e)
    <不过,strong>不应该有什么不同

  • 与其直接调用编译器,不如调用一个shell脚本,将stderr/stdout重定向到一个文件(或者使用
    cc…|&tee/tmp/cc.log
    保存一个副本),以确保复杂设置的任何部分都不包含重要信息


  • (1) 我按照你的建议做了,退出代码是0(2)。好的,整个错误对象在这里输出。所以e代码是假的(是的,这让我最难堪)(3)不。没有区别(4)问题是,我不需要这个操作的stdout,因为大多数情况下,stdout和stderr将为null,因为这是有效的Ccode@ShrikrishnaHolla你的节点的版本是什么?@xiaoyi:v0.9.6-pre(我最近从github克隆了它)(2)听起来你应该用测试用例提交一份bug报告。另外,由于您有源代码,请尝试找到实现
    exec()
    的位置,并检查它的实际功能。也许文档已经过时,如果(e.code)正确,那么现在就可以了。@ShrikrishnaHolla您用稳定的版本尝试过相同的代码吗?比如说0.8.17?
    { [Error: Command failed: ] killed: false, code: false, signal: undefined }