10

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

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

4 Comments

Same output as what you've posted, but actually running it produces the newline. Is this from my terminal? I doubt it because same examples written in C/C++ do not add that newline. Is it standard behaviour from node/JavaScript?
@Ælex no this is not standard behavior in node/JavaScript, it is likely something else like you are saying (terminal, environment variables, etc.)
I think you are not distinguishing the output from node itself from your terminal's behavior
you mean node -e 'process.stdout.write("123")' | wc -c ; )
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.