Execute command from node js application

2019-08-09 11:30发布

问题:

I use the following code to excute command from node application and this is working...The problem is when there is error I dont get it,I try to debug the code and I when I put break-point in the if(error) It doesnt stops ,any idea why?

var exec = require('child_process').exec;
var cmd = 'npm install winston --save';

exec(cmd, function(error, stdout, stderr) {
    if(error){
        console.log(error);
    }
});

回答1:

http://krasimirtsonev.com/blog/article/Nodejs-managing-child-processes-starting-stopping-exec-spawn According to this source, where the author tries a different method, you should try to change your exec part to the one on the following:

exec(cmd, function(error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});