执行 child_process.exec 时的 NodeJS 错误

NodeJS bug, when executing child_process.exec

我在这里错过了什么?

我想在代码中获取 cucumber-js 进程的结果。在 JSON 格式中:

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)

在结果中,我得到的标准输出不足,并且 json(结果)无效

当我换线时,- 一切正常:

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

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

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

您可以尝试使用 maxBuffer 选项。

阅读更多:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

此代码示例也可能有用:

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
});