84

I've been searching how to do it in other languages and I've found that I have to use the special character \b to remove the last character. (how-do-i-erase-printed-characters-in-a-console-applicationlinux)

This doesn't work for node.js in multiple calls to console.log ();

If I write a single log:

console.log ("abc\bd");

I get the result: abd

But if I write:

console.log ("abc");
console.log ("\bd");

I get the result:

abc
d

My goal is to print a waiting message like:

Waiting
Waiting.
Waiting..
Waiting...

and again:

Waiting
Waiting.
etc

all in the same line.

Community
  • 1
  • 1
Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112

6 Answers6

145

There are functions available for process.stdout:

var i = 0;  // dots counter
setInterval(function() {
  process.stdout.clearLine();  // clear current text
  process.stdout.cursorTo(0);  // move cursor to beginning of line
  i = (i + 1) % 4;
  var dots = new Array(i + 1).join(".");
  process.stdout.write("Waiting" + dots);  // write text
}, 300);

It is possible to provide arguments to clearLine(direction, callback)

/**
 * -1 - to the left from cursor
 *  0 - the entire line // default
 *  1 - to the right from cursor
 */

Update Dec 13, 2015: although the above code works, it is no longer documented as part of process.stdin. It has moved to readline

Qwerty
  • 29,062
  • 22
  • 108
  • 136
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    I expect that under the hood the '\r' character is enabling this. This character returns the cursor to the beginning of the line without starting new line. – Casey Watson Jul 26 '13 at 03:41
  • Not on windows though. – Adam K Dean Mar 13 '14 at 14:33
  • 7
    @pimvdb Where are these functions documented? They don't appear to be in the Node docs: http://nodejs.org/api/process.html#process_process_stdout – Henry Merriam Jun 19 '14 at 20:55
  • 6
    This doesn't seem to work on OSX, does anyone know if it should? – Markus Hedlund Oct 17 '14 at 12:41
  • 2
    It's possible the API changed. This answer was written two years ago, and any JS developer should at least have some sort of idea how much has changed in the last two years. We've gone from a few compile-to-JS languages (most notably CoffeeScript) to transpiling the newest version coming out (that's nearing publishing), ES6. Two years ago, if I recall correctly, ES6 modules didn't even exist. – Claudia Dec 14 '14 at 13:43
  • Can confirm `.cursorTo()` is still functional. – Julian H. Lam May 13 '15 at 17:55
  • 1
    This does not work when the line had been split in the shell (i. e. was over 80 characters). In that case only the last row is wiped. Do you know of any way to wipe everyting until the last newline? – tomekwi Oct 22 '15 at 09:26
  • Hm, this works in Node 4.0 but it does not work in Node 1.12.1. I think (the version that ships with meteor). I think the `readline` library has this functionality though. – corvid Nov 06 '15 at 14:06
19

Now you could use readline library and its API to do this stuff.

Sergey Kamardin
  • 1,640
  • 1
  • 18
  • 22
19

The easiest way to overwrite the same line is

var dots = ...
process.stdout.write('Progress: '+dots+'\r');

the \r is the key. It will move the cursor back to the beginning of the line.

jonnysamps
  • 1,067
  • 1
  • 14
  • 20
3

This works for me:

process.stdout.write('\033c');
process.stdout.write('Your text here');
Michael Yurin
  • 1,021
  • 14
  • 18
1

Using a Carriage Return

The carriage return character places the cursor back at the beginning of the current line.

process.stdout.write("\r");

This solution worked for me — I only tested it with a single character.

Andria
  • 4,712
  • 2
  • 22
  • 38
Lolo
  • 53
  • 1
  • 9
  • Carriage Return is incorrect here as it erases the whole line. If you replace it with `\b` in your answer it will work. Also, make sure to update your example so that someone reading it can immediately understand your example (here I'm wondering: why not trying to erase a character to demonstrate it's working properly?) – aymericbeaumet May 01 '20 at 23:25
-3

Try by moving the \r at the start of the string, this worked on Windows for me:

for (var i = 0; i < 10000; i+=1) {
    setTimeout(function() {
        console.log(`\r ${i}`);
    }, i);
}
daremkd
  • 8,244
  • 6
  • 40
  • 66
  • 2
    This won't work - `console.log` adds a `\n` to the end of your string. You need to use `process.stdout.write` – Sethi Jan 27 '17 at 11:03