trogne trogne - 4 months ago 267
Node.js Question

node.js stdout clearline() and cursorTo() functions

From a node.js tutorial, I see those two process.stdout functions :

process.stdout.clearLine();
process.stdout.cursorTo(0);


But I'm using a more recent node.js version (4.2.4), and those functions do not exist. I get
process.stdout.clearLine is not a function
and
process.stdout.cursorTo is not a function
.

What is the equivalent of clearLine and cursorTo on node.js version 4.2.4 ?

EDIT :

Those are not working either :

process.readline.clearLine();
process.readline.cursorTo(0);

function writeWaitingPercent(p) {
process.readline.clearLine();
process.readline.cursorTo(0);
process.stdout.write(`waiting ... ${p}%`);
}


I get
Cannot read property 'clearLine' of undefined

Answer

This is the solution :

First, require readline :

var readline = require('readline');

Then, use cursorTo like this :

function writeWaitingPercent(p) {
    //readline.clearLine(process.stdout);
    readline.cursorTo(process.stdout, 0);
    process.stdout.write(`waiting ... ${p}%`);
}

I've commented clearLine, since it's useless in my case (cursorTo move back the cursor to the start)