Stdout buffer issue using node child_process

后端 未结 3 785
無奈伤痛
無奈伤痛 2020-12-13 01:30

I\'m trying to execute curl using node child_process to get a JSON file (about 220Ko) from a shared folder in a local network. But it actually returns a buffer problem that

相关标签:
3条回答
  • 2020-12-13 01:44

    I had a similar issue and I fixed it moving from exec to spawn:

    var child = process.spawn('<process>', [<arg1>, <arg2>]);
    
    child.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    });
    
    child.stderr.on('data', function (data) {
      console.log('stderr: ' + data);
    });
    
    child.on('close', function (code) {
        console.log('child process exited with code ' + code);
    });
    
    0 讨论(0)
  • 2020-12-13 01:58

    You need to use and set the maxBuffer option when using child_process.exec. From the documentation:

    maxBuffer specifies the largest amount of data allowed on stdout or stderr - if this value is exceeded then the child process is killed.

    The documentation also states that the default value of maxBuffer is 200KB.

    As an example, the maximum buffer size is increased to 500KB in the following code:

    var execute = function(command, callback){
        exec(command, {maxBuffer: 1024 * 500}, function(error, stdout, stderr){ callback(error, stdout); });
    };
    

    Additionally, you may want to read about http.get to see if it is capable of achieving what you are trying to do.

    0 讨论(0)
  • 2020-12-13 02:09

    Adding some explanation to the answers.

    The exec command buffers the data before sending it to the parent process. It is generally suitable for the commands producing the smaller output. The above error occurred because the output generated by the execution of the command was larger than the max buffer size. One way to solve the above issue is to specify the buffer size as answered by Tim Cooper.

    var execute = function(command, callback){
    exec(command, {maxBuffer: 1024 * 500}, function(error, stdout, stderr){ 
     callback(error, stdout); });
    };
    

    Another solution is to use the spawn method which is generally faster compared to the exec and it does not buffer the data before sending. It sends the data as a stream hence the problem of buffer size never occurs. The code snippet used by Isampaio.

    var child = process.spawn('<process>', [<arg1>, <arg2>]);
    child.stdout.on('data', function (data) {
     console.log('stdout: ' + data);
    });
    child.stderr.on('data', function (data) {
     console.log('stderr: ' + data);
    });
    child.on('close', function (code) {
     console.log('child process exited with code ' + code);
    });
    
    0 讨论(0)
提交回复
热议问题