Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!