Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clear console/terminal in node.js the right way
const readline = require('readline')
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
@EdgardoRodriguezSolano

This comment has been minimized.

Copy link

@EdgardoRodriguezSolano EdgardoRodriguezSolano commented Jan 11, 2020

Have you tried console.clear() ?

@pranesh239

This comment has been minimized.

Copy link

@pranesh239 pranesh239 commented Jan 26, 2020

Have you tried console.clear() ?

it worked perfectly fine for me!

@jonathan-annett

This comment has been minimized.

Copy link

@jonathan-annett jonathan-annett commented Feb 19, 2020

//this also stops someone scrolling back and viewing sensitive data that may have been logged
function clearConsoleAndScrollbackBuffer() {
process.stdout.write("\u001b[3J\u001b[2J\u001b[1J");console.clear();
}

@timneutkens

This comment has been minimized.

Copy link
Owner Author

@timneutkens timneutkens commented Feb 19, 2020

@EdgardoRodriguezSolano you're not preserving history if you do that.

@Yash-Singh1

This comment has been minimized.

Copy link

@Yash-Singh1 Yash-Singh1 commented Aug 25, 2020

console.clear() removes all terminal data. Not the current stdout.

@Dmitriy2020-altair

This comment has been minimized.

Copy link

@Dmitriy2020-altair Dmitriy2020-altair commented Dec 4, 2020

Bravo! it works.... Professional!

@MahefaAbel

This comment has been minimized.

Copy link

@MahefaAbel MahefaAbel commented Mar 5, 2021

console.clear()

It doesn't work for me

const readline = require('readline')
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)

And that to

I wonder what kind of console you clear with theses approach but none of them does not works for me.

nodejs-output-console

As you can see in the image, my whole node output console still there.

@hangtwenty

This comment has been minimized.

Copy link

@hangtwenty hangtwenty commented Apr 13, 2021

Side note on this topic: I used the solution in the gist, but I got curious about something. I checked the source code for rollup --watch since that includes an option to clear the screen. I noticed it uses '\u001Bc' (which the beginning of @jonathon-annett's snippet above). Just noting.

@hangtwenty

This comment has been minimized.

Copy link

@hangtwenty hangtwenty commented Apr 13, 2021

Another note, if you're using nodemon and want to clear the console when nodemon restarts; here is a relevant tip:

This is what the nodemon events are for. You can include either a global or local nodemon.json file as per the following to do what you're after.

As per the design principles, I'd rather re-use the current tech rather than introduce new features:

{
  "events": {
    "start": "echo \"\\x1Bc\""
  }
}

It is from this thread, which has more info about portability if you must deal with Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment