Node.Js on windows - How to clear console

泪湿孤枕 提交于 2019-11-27 11:04:21
sanatgersappa
console.log('\033[2J');

This works on linux. Not sure about windows.

You can "trick" the user using something like this:

var lines = process.stdout.getWindowSize()[1];
for(var i = 0; i < lines; i++) {
    console.log('\r\n');
}
process.stdout.write('\033c');

This also works on windows. Win7 at least.

laktak

This clears the console on Windows and puts the cursor at 0,0:

var util = require('util');
util.print("\u001b[2J\u001b[0;0H");

or

process.stdout.write("\u001b[2J\u001b[0;0H");

This is for Linux mainly but is also reported to work in Windows.

There is Ctrl + L in Gnome Terminal that clears the terminal as such. It can be used with Python, Node JS or any Interpreter presumably that uses terminal. I tend to clear many times hence this is very handy. Instaed of doing clear in Gnome Terminal you can just do Ctrl + L, it has nothing to do with the REPL running.

And to clear the console while in strict mode on Windows:

'use strict';
process.stdout.write('\x1Bc'); 

i am using a windows CMD and this worked for me

console.clear();

Just use CTRL + L on windows to clear the console.

Haven't tested this on Windows but works on unix. The trick is in the child_process module. Check the documentation. You can save this code as a file and load it to the REPL every time you need it.

var util = require('util');
var exec = require('child_process').exec;

function clear(){
    exec('clear', function(error, stdout, stderr){
        util.puts(stdout);
    });    
}
Henri Cavalcante

To solve problems with strict mode:

'use strict';
process.stdout.write('\x1B[2J');

I couldn't get any of the above to work. I'm using nodemon for development and found this the easiest way to clear the console:

  console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

It just scrolls the console several lines so you get a clear screen for subsequent console.log commands.

Hope it helps someone.

If you're using VSCode you can use CTRL + K. I know this is not a generic solution but may help some people.

Based on sanatgersappa's answer and some other info I found, here's what I've come up with:

function clear() {
    var stdout = "";

    if (process.platform.indexOf("win") != 0) {
        stdout += "\033[2J";
    } else {
        var lines = process.stdout.getWindowSize()[1];

        for (var i=0; i<lines; i++) {
            stdout += "\r\n";
        }
    }

    // Reset cursur
    stdout += "\033[0f";

    process.stdout.write(stdout);
}

To make things easier, I've released this as an npm package called cli-clear.

There is no console.clear() in node.

With ES6 JavaScript received the ''.repeat() string method, and so we can do:

console.log('\n'.repeat(1000));

which will basically print 1000 empty lines and simulate a console.clear()

Belated, but ctrl+l works in windows if you're using powershell :) Powershell + chocolatey + node + npm = winning.

This code works fine on my node.js server console Windows 7.

process.stdout.write("\u001b[0J\u001b[1J\u001b[2J\u001b[0;0H\u001b[0;0W");

In my case I did it to loop for ever and show in the console a number ever in a single line:

class Status {

  private numberOfMessagesInTheQueue: number;
  private queueName: string;

  public constructor() {
    this.queueName = "Test Queue";
    this.numberOfMessagesInTheQueue = 0;
    this.main();
  }

  private async main(): Promise<any> {    
    while(true) {
      this.numberOfMessagesInTheQueue++;
      await new Promise((resolve) => {
        setTimeout(_ => resolve(this.showResults(this.numberOfMessagesInTheQueue)), 1500);
      });
    }
  }

  private showResults(numberOfMessagesInTheQuee: number): void {
    console.clear();
    console.log(`Number of messages in the queue ${this.queueName}: ${numberOfMessagesInTheQuee}.`)
  }
}

export default new Status();

When you run this code you will see the same message "Number of messages in the queue Test Queue: 1." and the number changing (1..2..3, etc).

AmishJohn81

Ctrl + L This is the best, simplest and most effective option.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!