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?