trogne
trogne

Reputation: 3552

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

Upvotes: 46

Views: 44638

Answers (6)

Mert Sevinc
Mert Sevinc

Reputation: 1027

I just want to point out that you may also face this problem when running your Node application on Visual Studio Code! Running the same application on terminal will just work perfectly.

Upvotes: 0

Quentin Grisel
Quentin Grisel

Reputation: 4987

For my part, I had the clearline is not a function error in a package that use process.stdout.clearline().

I had to start my windows console as administrator to make it works.

Upvotes: 0

Maksim Shamihulau
Maksim Shamihulau

Reputation: 1738

if you see stdout exceptions like TypeError: process.stdout.clearLine is not a function in Debug Console window of Visual Studio Code (or Webstorm), run the app as external terminal application instead of internal console. The reason is that Debug Console window is not TTY (process.stdout.isTTY is false). Therefore update your launch configuration in launch.json with "console": "externalTerminal" option.

Upvotes: 6

Imanou Petit
Imanou Petit

Reputation: 92549

The Readline module that is part of Node.js now provides readline.cursorTo(stream, x, y), readline.moveCursor(stream, dx, dy) and readline.clearLine(stream, dir) methods.


With TypeScript, your code should look like this:

import * as readline from 'readline'
// import readline = require('readline') also works

/* ... */

function writeWaitingPercent(p: number) {
    readline.clearLine(process.stdout, 0)
    readline.cursorTo(process.stdout, 0, null)
    let text = `waiting ... ${p}%`
    process.stdout.write(text)
}

The previous code will transpile into the following Javascript (ES6) code:

const readline = require('readline');

/* ... */

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

As an alternative, you can use the following code for Javascript (ES6):

const readline = require('readline');

/* ... */

function waitingPercent(p) {
    readline.clearLine(process.stdout, 0)
    readline.cursorTo(process.stdout, 0)
    let text = `waiting ... ${p}%`
    process.stdout.write(text)
}

Upvotes: 31

trogne
trogne

Reputation: 3552

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)

Upvotes: 59

Rafael Teixeira
Rafael Teixeira

Reputation: 49

process.readline.cursorTo and process.readline.clearLine

Node v4.2.4 Documentation

Upvotes: -8

Related Questions