Why does Node.js' fs.readFile() return a buffer instead of string?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why does Node.js' fs.readFile() return a buffer instead of string?



I'm trying to read the content of test.txt(which is on the same folder of the Javascript source) and display it using this code:


test.txt


var fs = require("fs");

fs.readFile("test.txt", function (err, data)
if (err) throw err;
console.log(data);
);



The content of the test.txt was created on nano:


test.txt


nano



Testing Node.js readFile()



And I'm getting this:


Nathan-Camposs-MacBook-Pro:node_test Nathan$ node main.js
<Buffer 54 65 73 74 69 6e 67 20 4e 6f 64 65 2e 6a 73 20 72 65 61 64 46 69 6c 65 28 29>
Nathan-Camposs-MacBook-Pro:node_test Nathan$




6 Answers
6



From the docs:



If no encoding is specified, then the raw buffer is returned.



Which might explain the <Buffer ...>. Specify a valid encoding, for example utf-8, as your second parameter after the filename. Such as,


<Buffer ...>


utf-8


fs.readFile("test.txt", "utf8", function(err, data) ...);



try


fs.readFile("test.txt", "utf8", function(err, data) ...);



basically you need to specify the encoding.





According to the docs it's "utf8" not "utf-8".
– Marc
Nov 29 '16 at 8:32


"utf8"


"utf-8"



It is returning a Buffer object.



If you want it in a string, you can convert it with data.toString():


data.toString()


var fs = require("fs");

fs.readFile("test.txt", function (err, data)
if (err) throw err;
console.log(data.toString());
);





Kind of old, but it should be known that this solution introduces extra overhead since buffer.toString() assumes utf-8 encoding anyway. Thus, this would be equivelant to (though, slower than) @hvgotcodes' answer.
– Brandon
Jul 23 '13 at 21:15



buffer.toString()



Async:


fs.readFile('test.txt', 'utf8', callback);



Sync:


var content = fs.readFileSync('test.txt', 'utf8');



The data variable contains a Buffer object. Convert it into ASCII encoding using the following syntax:


data


Buffer


data.toString('ascii', 0, data.length)



Asynchronously:


fs.readFile('test.txt', 'utf8', function (error, data)
if (error) throw error;
console.log(data.toString());
);



This comes up high on Google, so I'd like to add some context information about the original question (emphasis mine):



Why does Node.js' fs.readFile() return a buffer instead of string?



Even if you as the programmer know it: Node has no idea what's in the file you're trying to read. It could be a text file, but it could just as well be a ZIP archive or a JPG image — Node doesn't know.



Even if Node knew it were to read a text file, it still had no idea which character encoding is used (i.e. how the bytes in the file map to human-readable characters), because the character encoding itself is not stored in the file.



There are ways to guess the character encoding of text files with more or less confidence (that's what editors usually do), but you usually don't want your code to rely on guesses without your explicit intention.



So, because it does not and can not know all these details, Node just reads the file byte for byte, without assuming anything about its contents.



That's what the returned buffer is: an unopinionated container for raw binary content. How this content should be interpreted is up to you as the developer.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS