Ælex
Ælex

Reputation: 14829

Node.JS write to stdout without a newline

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");

Upvotes: 10

Views: 12406

Answers (2)

Peter Lyons
Peter Lyons

Reputation: 145994

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

Upvotes: 8

zangw
zangw

Reputation: 48346

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...

Upvotes: 7

Related Questions