Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS write to stdout without a newline

Tags:

node.js

stdout

I have the feeling that is probably not possible: I am trying to print on the terminal text without a new line. I have tried process.stdout.write and npm jetty but they all seem to automatically append a new line at the end.

Is it possible to write to stdout without having an automatic newline? Just to be clear: I am not concerned about browsers, I am only interested in UNIX/Linux writing what in C/C++ would be the equivalent of:

std::cout << "blah";
printf("blah");
like image 666
Ælex Avatar asked Jan 22 '16 23:01

Ælex


2 Answers

process.stdout.write() does not automatically add a new line. If you post precise details about why you think it does, we can probably tell you how you are getting confused, but while console.log() does add a newline, process.stdout.write() has no frills and will not write anything you don't explicitly pass to it.

Here's a shell session providing supporting evidence:

echo 'process.stdout.write("123")' > program.js

node program.js | wc -c
       3
like image 133
Peter Lyons Avatar answered Oct 19 '22 20:10

Peter Lyons


According to this link process.stdout.write():

console.log equivalent could look like this:

console.log = function(msg) {
  process.stdout.write(`${msg}\n`);
};

So process.stdout.write should meet your request...

like image 34
zangw Avatar answered Oct 19 '22 21:10

zangw