Node.Js on windows - How to clean console

Being completely new in the environment and philosophy of node.js, I would like to get answers to a few questions. I downloaded node.js for windows installer as well as node package manager. Currently, the Windows Cmd command is used to run nodejs applications.

  • cls clears the command window or errors on the command line. Is there an equivalent for node.js? console.clear does not exist (or does it in some other form?

  • I created a server through this code below

    var http = require("http"); http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/html" }); response.write("Hello World"); console.log("welcome world")response.end(); }).listen(9000, "127.0.0.1"); 

I changed the code below and updated the browser to find out that the content type is not changing, how can I see the changes?

 var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); console.log("welcome world") response.end(); }).listen(9000,"127.0.0.1"); 
+76
windows windows-vista console
Jan 25 '12 at 17:15
source share
18 answers
 console.log('\033[2J'); 

This works on linux. Not sure about the windows.

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

 var lines = process.stdout.getWindowSize()[1]; for(var i = 0; i < lines; i++) { console.log('\r\n'); } 
+92
Feb 26 '12 at 12:15
source share
 process.stdout.write('\033c'); 

This also works with windows. Win7 at least.

+67
Oct 15 '14 at 3:23
source share

This clears the console from Windows and places 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"); 
+52
Feb 20 '13 at 9:48 a.m.
source share

This is for Linux , but is reportedly working on Windows.

In the Gnome terminal, there is Ctrl + L , which clears the terminal as such. It can be used with Python, Node JS, or any interpreter supposedly using a terminal. I am inclined to reason many times, therefore it is very convenient. Created in the Gnome terminal, you can just do Ctrl + L , it has nothing to do with running REPL.

+39
Jun 07
source share

And clear the console in strict mode on Windows:

 'use strict'; process.stdout.write('\x1Bc'); 
+25
Oct 02 '15 at 2:26
source share

I am using Windows CMD and it worked for me

 console.clear(); 
+17
Mar 29 '18 at 15:53
source share

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

+8
May 03 '16 at 18:12
source share

I have not tested this on Windows, but it works on unix. The trick is in the child_process module. Check the documentation. You can save this code as a file and upload it to 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); }); } 
+7
Dec 01
source share

To solve strict mode issues:

 'use strict'; process.stdout.write('\x1B[2J'); 
+3
Jun 06 '15 at 2:00
source share

I could not get any of the above to work. I use nodemon for development and found this the easiest way to clean up the console:

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

It simply scrolls the console a few lines to get a clear screen for subsequent console.log commands.

Hope this helps someone.

+2
Apr 04
source share

If you use VSCode you can use CTRL + K I know this is not a general solution, but it may help some people.

+2
Jan 20 '18 at 11:36
source share

Based on sanatgersappa's answer and some other information I found, here is what I came 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 released this as an npm package called cli-clear .

+1
Jan 22 '14 at 14:27
source share

There is no console.clear() in node.

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

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

which will basically print 1000 blank lines and mimic a console.clear()

+1
Aug 11 '17 at 10:16 on
source share

Delayed, but ctrl + l works in windows if you use powershell :) Powershell + chocolatey + node + npm = win.

0
Jan 05 '15 at 19:56
source share

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

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

0
Jul 21 '16 at 0:21
source share

In my case, I did this to loop and show in the console a number on one 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 change of number (1..2..3, etc.).

0
Jul 22 '19 at 0:36
source share

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

-one
May 17 '16 at 18:55
source share

If you use the Windows cls command in the console

-four
Apr 15 '15 at 16:25
source share



All Articles