I have a file temp.js that contains text as below:
module.config = {
key1 : 'value1',
key2 : 'value2',
key3 : ['abc','def']
}
I am reading the file as below:
fs.readFile('/temp.js', function(err,fileContents) {
console.log(fileContents);
});
on the output I am getting values as below:
<Buffer 6d 6f .....
...>
What am i missing here ?
From the documentation for fs.readFile:
If no encoding is specified, then the raw buffer is returned.
Therefore, if you want a string passed to your callback, specify the encoding of the file. Example:
fs.readFile('/temp.js', {encoding: 'utf8'}, function(err, fileContents) {
console.log(fileContents);
});
The file is being read but as Buffer. To read it as string you've to tell fs the encoding.
Try this
require('fs').readFile('/temp.js','utf-8', function(err,fileContents) {
console.log(fileContents);
});