Slava Melnik
Slava Melnik

Reputation: 103

NodeJS bug, when executing child_process.exec

What am I missing here?

I want to get results of cucumber-js process in code. In JSON format:

const { exec } = require('child_process')

function getResults () {
  return new Promise((resolve, reject) => {
    const options = {
      encoding: 'utf8'
    };
    let results = '';
    let commands = 'cucumber-js —format json';

    const proc = exec(commands, options);

    proc.stdout.setEncoding('utf8');
    proc.stdout.on('data', chunk => { results += chunk });
    proc.stdout.on('end', () => { resolve(results) });
    proc.stdout.on('error', reject);
  })
}

getResults().then(console.log)

In results i get not enough stdout, and json (results) is not valid

When i change line,- all are working:

let commands = 'cucumber-js —format json';

==============>

let commands = 'cucumber-js —format json > results.txt && cat results.txt';

Upvotes: 1

Views: 604

Answers (1)

Denis Bendrikov
Denis Bendrikov

Reputation: 36

You can try to play with maxBuffer option.

Read more: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Also this code sample might be useful:

var exec = require('child_process').exec;
var child = exec('npm ls --json --long', {
    encoding: 'utf8',
    // maxBuffer Number largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (Default: 200*1024)
    maxBuffer: 10*1024*1024
});

Upvotes: 2

Related Questions