Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js — Clear Terminal / Console. Reset to initial state.
console.reset = function () {
return process.stdout.write('\033c');
}
@nawazdj

This comment has been minimized.

Copy link

nawazdj commented Sep 13, 2015

This is for clearing the console purpose, right? This does NOT work. I tried on windows version though.

@priyadarshikunal

This comment has been minimized.

Copy link

priyadarshikunal commented Nov 14, 2015

This works on Terminal/iTerm on OS X

@pendechosen

This comment has been minimized.

Copy link

pendechosen commented Nov 17, 2015

Works on my windows 7.

@naoxink

This comment has been minimized.

Copy link

naoxink commented Jan 7, 2016

Works on my debian 7 :D

@kocoten1992

This comment has been minimized.

Copy link

kocoten1992 commented Apr 10, 2016

work on ubuntu, node v5.10.0

P/s: or just simply console.log('\033c') will do

@jimmywarting

This comment has been minimized.

Copy link

jimmywarting commented Apr 28, 2016

don't work in strict mode on node v6
But write("\x1B[2J") did

@adongoj

This comment has been minimized.

Copy link

adongoj commented May 10, 2016

Thanks, it worked for me. don't forget to call the function

@DesignByOnyx

This comment has been minimized.

Copy link

DesignByOnyx commented Jul 1, 2016

Node 5 strict mode requires this, which is the same as jimmywarting's answer but it also resets the cursor:

process.stdout.write('\x1B[2J\x1B[0f');
@davesnx

This comment has been minimized.

Copy link

davesnx commented Jul 23, 2016

@DesignByOnyx Thanks man!

@mocon

This comment has been minimized.

Copy link

mocon commented Aug 4, 2016

Sweet! Thanks.

@TemaSM

This comment has been minimized.

Copy link

TemaSM commented Aug 7, 2016

Strict mode + Node.js all supported platforms:

'use strict';
process.stdout.write('\x1Bc');
// or
console.log('\x1Bc');
@SLeonescu

This comment has been minimized.

Copy link

SLeonescu commented Sep 23, 2016

or you could use the key combinations Ctrl + L

@KhaledSMQ

This comment has been minimized.

Copy link

KhaledSMQ commented Sep 29, 2016

Thank you @DesignByOnyx

process.stdout.write('\x1B[2J\x1B[0f\u001b[0;0H');

@TheRealCasadaro

This comment has been minimized.

Copy link

TheRealCasadaro commented Jul 27, 2017

what is the '\x1Bc' it look like a regular expression, why does it clear the console

@NLDev

This comment has been minimized.

Copy link

NLDev commented Aug 10, 2017

@TheRealCasadaro It's an ANSI Escape Sequence which clears the screen and buffer for the terminal output. It basically becomes <ESC>c which is the VT100 escape code for resetting the terminal.

http://www.termsys.demon.co.uk/vtansi.htm
https://en.wikipedia.org/wiki/ANSI_escape_code

@aaalsubaie

This comment has been minimized.

Copy link

aaalsubaie commented Sep 13, 2017

thank you it works like a charm

@mocon

This comment has been minimized.

Copy link

mocon commented Oct 1, 2017

Thank you @DesignByOnyx!

@tomasevich

This comment has been minimized.

Copy link

tomasevich commented Dec 5, 2017

Works on my debian 8 :D

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 14, 2017

Works perfectly on Windows 10 Node.js v8.9.1.
Thanks.

@lockevn

This comment has been minimized.

Copy link

lockevn commented Jan 21, 2018

Thank you @DesignByOnyx
It works on Windows 10 Node.js v.8.5

@megtorres

This comment has been minimized.

Copy link

megtorres commented Jan 22, 2018

@SLeonescu Wow, thanks, that's so easy!

@pablohpsilva

This comment has been minimized.

Copy link

pablohpsilva commented Mar 7, 2018

Thank you @TemaSM

@joseluisq

This comment has been minimized.

Copy link

joseluisq commented Mar 28, 2018

or via package.json:

"scripts": {
+  "cls_opt1": "clear",
+  "cls_opt2": "node -e \"process.stdout.write('\\033c')\""
}
@femi-dd

This comment has been minimized.

Copy link

femi-dd commented May 19, 2018

console.clear() does the trick without having to use keyboard shortcuts every time you run your js files

@ktrzeciaknubisa

This comment has been minimized.

Copy link

ktrzeciaknubisa commented Jul 4, 2018

The below is better, because it clears also scroll-back buffer

process.stdout.write('\033c\033[3J');

http://man7.org/linux/man-pages/man4/console_codes.4.html

ESC [ 3 J: erase whole display including scroll-back buffer (since Linux 3.0).

@olvrb

This comment has been minimized.

Copy link

olvrb commented Aug 29, 2018

@DesignByOnyx's answer works on Ubuntu bash on Windows 10.

@tutlane

This comment has been minimized.

Copy link

tutlane commented Oct 11, 2018

Thanks @ghost. For me Ctrl + L worked to clear node.js console.

@anriDo

This comment has been minimized.

Copy link

anriDo commented Nov 25, 2018

work on MacOS Mojave

@silavsale

This comment has been minimized.

Copy link

silavsale commented Dec 1, 2018

Node 5 strict mode requires this, which is the same as jimmywarting's answer but it also resets the cursor:

process.stdout.write('\x1B[2J\x1B[0f');

Thanks, its work on windows 10

@mishhubc

This comment has been minimized.

Copy link

mishhubc commented Jan 20, 2019

The combinations Ctrl + L reset the full log.

Ctrl + L to clear screen - https://unix.stackexchange.com/questions/104094/is-there-any-way-to-enable-ctrll-to-clear-screen-when-set-o-vi-is-set

thanks

@mybouhssina

This comment has been minimized.

Copy link

mybouhssina commented Mar 7, 2019

Thank you.
If anyone is wondering when/how to call the function, the following is working for me :

app.listen(process.env.PORT || 3000,function (){
  process.stdout.write('\033c'); 
}

since app.listen accepts a callback method, hope this helps someone ! :)

@aacassandra

This comment has been minimized.

Copy link

aacassandra commented May 17, 2019

Thank you @TemaSM, its beautiful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.