Retrieve bash error from NodeJS child_process.spawn

大兔子大兔子 提交于 2020-01-16 06:11:04

问题


When I execute the (made up and ridiculous) command banana from bash I get the following error:

bash: banana: command not found

But if I execute banana in a child_process.spawn in Node.js, I get the error:

net.js:624
    throw new TypeError('invalid data');
          ^
TypeError: invalid data
    at WriteStream.Socket.write (net.js:624:11)
    at ChildProcess.<anonymous> 
    at ChildProcess.emit (events.js:107:17)
    at Process.ChildProcess._handle.onexit (child_process.js:1065:12)
    at child_process.js:1137:20
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3

Is there any way to get the text of the original bash error from Node.JS?


回答1:


Attaching to the 'error' event emitter for the child_process.spawn will give you the following message:

child_process_instance.on('error', function (error) {
    console.log(error);
});

Error: spawn bananas ENOENT


来源:https://stackoverflow.com/questions/29619530/retrieve-bash-error-from-nodejs-child-process-spawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!