child_process.exec的回调参数的差异
发布于 8 年前 作者 EchoFUN 4591 次浏览 来自 问答

请教一个小问题: 我使用了childprocess的exec方法执行了某个系统命令。官方的文档上exec方法,他的回调函数里有三个参数,error, stdout, stderr;只提到了error是个对象,stdout和stderr是stream的格式;但是没有具体的参数的解释。 我在执行了exec方法的时候,报错了。接着我打印了stderr和error.message发现返回的错误信息不一样;所以请问,这个stderr和error有什么区别呢? 有个猜测,是不是error是从node.js程序的角度看待这个错误,而stderr是从这个命令(系统)的角度返回的这个错误的相关信息? 求高手指点~

1 回复

不是高手. 我是这么理解的

callback Function called with the output when process terminates error Error stdout Buffer stderr Buffer

The callback gets the arguments (error, stdout, stderr). On success, error will be null. On error, error will be an instance of Error and error.code will be the exit code of the child process, and error.signal will be set to the signal that terminated the process.

error我跟你的理解一样, 是node进程为你生成的错误对象; 而stdoutstderr分别是子进程输出到自己的stdout和stderr的内容(Buffer)

回到顶部