Node.js: printing to console without a trailing newline?
Also, if you want to overwrite messages in the same line, for instance in a countdown, you could add \r
at the end of the string.
process.stdout.write("Downloading " + data.length + " bytes\r");
As an expansion/enhancement to the brilliant addition made by @rodowi above regarding being able to overwrite a row:
process.stdout.write("Downloading " + data.length + " bytes\r");
Should you not want the terminal cursor to be located at the first character, as I saw in my code, the consider doing the following:
let dots = ''process.stdout.write(`Loading `)let tmrID = setInterval(() => { dots += '.' process.stdout.write(`\rLoading ${dots}`)}, 1000)setTimeout(() => { clearInterval(tmrID) console.log(`\rLoaded in [3500 ms]`)}, 3500)
By placing the \r
in front of the next print statement the cursor is reset just before the replacing string overwrites the previous.