Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Node.js Can't Redirect All Output From Event Emitter

I have a very simple JavaScript file, c.js:

const cliProgress = require("cli-progress");

const bar = new cliProgress.SingleBar(
    {
        stream: process.stdout
    },
    cliProgress.Presets.shades_classic
);
bar.on('start', () => {
    console.log('started');
});

bar.start(100, 0);

let progress = 0;

function update(){
    progress += 20;
    bar.update(progress);
    if (progress < 100){
        setTimeout(update, 1000);
    } else {
        bar.stop();
        console.log('stopped');
    }
}

update();

If I run this program with node c.js, then the following gets printed to the console:

started
 ████████████████████████████████████████ 100% | ETA: 0s | 100/100
stopped

However, when I try to redirect the output to a file with node c.js > out.txt, then nothing is printed to the console (as expected), but the file out.txt contains only:

stopped

Why does the output file not contain "started" or any of the progress bar?

Since cli-progress is not my package, I have tried this with my own simple event emitter as well.

The file b.js has a simple event emitter:

const EventEmitter = require('events');

class B extends EventEmitter {

    ping(){
        this.emit('ping');
    }

}

module.exports = B;

And the file a.js is as follows:

const B = require('./b');

const b = new B();
b.on('ping', () => {
    console.log('ping');
})

let count = 0;

function doPing(){
    b.ping();
    if (++count < 3){
        setTimeout(doPing, 1000);
    }
}

doPing();

Running node a.js prints 3 "pings" to the console as expected:

ping
ping
ping

Running node a.js > out.txt also successfully redirects all the "pings" to the file.

Why does this work correctly with my simple example, but not with cli-progress. Is this an issue with the cli-progress package or something else?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda