Bryce Johnson Bryce Johnson - 1 year ago 149
Node.js Question

Why is fs.readFile returning a buffer?

I have referred to this question already. That is, I don't believe my problem lies in a misunderstanding of async.

Here is the relevant part of my module.

var fs = require('fs');
var q = require('q');
var u = require('../utils/json');

var indexFile = './data/index.json';

function getIndex() {
var def = q.defer(),
promise = def.promise,

fs.readFile(indexFile, function(err,data) {
if (err) {
throw err;
console.log('data', data);


return promise;

When I log 'data', I'm getting a buffer (below), rather than the JSON content of that file.

<Buffer 5b 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d 5d>

Any thoughts on why?

Answer Source

As per the Node.js API docs for 'fs' module, if the encoding option isn't passed, the read functions will return a buffer.

If you pass a value for encoding, it will return a string with that encoding:

fs.readFile('/etc/passwd', 'utf-8', callback)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download