Node stream buffers in console.log vs process.stdout.write
<p>Using <strong>NodeJS v5.6</strong> I created a file called <code>read-stream.js</code>:</p>

<pre><code>const
    fs = require('fs'),
    stream = fs.createReadStream(process.argv[2]);

stream.on('data', function(chunk) {
    process.stdout.write(chunk);
});

stream.on('error', function(err) {
    process.stderr.write("ERROR: " + err.message + "\n");
});
</code></pre>

<p>and a data file in plain text called <code>target.txt</code>:</p>

<pre><code>hello world
this is the second line 
</code></pre>

<p>If I do <code>node read-stream.js target.txt</code> the contents of <code>target.txt</code> are printed normally on my console and all is well.</p>

<p>However if I switch <code>process.stdout.write(chunk);</code> with <code>console.log(chunk);</code> then the result I get is this:</p>

<pre><code>&lt;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 69 6e 65 0a&gt;
</code></pre>

<p>I recently found out that by doing <code>console.log(chunk.toString());</code> the contents of my file are once again printed normally.</p>

<p>As per this <a href="https://stackoverflow.com/q/4976466/178728">question</a>, <code>console.log</code> is supposed to use <code>process.stdout.write</code> with the addition of a <code>\n</code> character. But what exactly is happening with encoding/decodings here?</p>

<p>Thanks in advance.</p>