Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error handling using child_process.execSync in NodeJs

This is my NodeJs snippet:

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

Here I am getting stdout as output to testoutput variable from the execSync process if the make process is completely exited. But, if the make test process encounters an stderr error then I am getting an error in NodeJs.

How can I store the stderr message to an output variable testoutput if the make process does not exit and store stdout in variable testoutput if the process exits well with stdout output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To catch errors with execSync(), you need a try/catch around your call as errors will throw an exception.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading