使用child_process处理错误.NodeJs中的execSync



这是我的NodeJs代码片段:

const {execSync} = require('child_process');
testoutput = execSync('make test');

这里我得到stdout作为输出到testoutput变量从execSync进程如果make进程完全退出。但是,如果make test进程遇到stderr错误,那么我在NodeJs中得到一个错误。

如果make进程没有退出,如何将stderr消息存储到输出变量testoutput,如果进程以stdout输出退出,如何将stdout存储在变量testoutput中。

要捕获execSync()的错误,您需要在调用周围使用try/catch,因为错误将抛出异常。