Node. js child_process exec return value truncated
In practice, a problem is encountered. When the system command is called using child_process exec, if the returned content of the command is large, the obtained stdout will be truncated. The following is the relevant code:
- Exec ("some command", function (error, stdout, stderr ){
- // Use the returned value stdout for related Calculation
- }
After investigation, we found that a buffer zone will be used during exec execution. The default size is 200 × 1024. So the solution is to increase the buffer size to the appropriate value:
- Exec ('dir/B/O-D ^ 2014 *',{
- MaxBuffer: 2000*1024 // quick fix
- }, Function (error, stdout, stderr ){
- List_of_filenames = stdout. split ('\ r \ n'); // adapt to your line ending char
- Console. log ("Found % s files in the replay folder", list_of_filenames.length)
- }
- )
For more information, see the official documentation.
Others also provide a solution, but it is not verified for reference only:
- Var exec = require('child_process'cmd.exe c;
- Function my_exec (command, callback ){
- Var proc = exec (command );
- Var list = [];
- Proc. stdout. setEncoding ('utf8 ');
- Proc. stdout. on ('data', function (chunk ){
- List. push (chunk );
- });
- Proc. stdout. on ('end', function (){
- Callback (list. join ());
- });
- }
- My_exec ('dir/s', function (stdout ){
- Console. log (stdout );
- })