dimanche 22 juin 2014

node.js readfile error with utf8 encoded file on windows


Vote count:

0




I'm trying to load a UTF8 json file from disk using node.js (0.10.29) on Windows 8.1. The following is the code that runs:



var http = require('http');
var utils = require('util');
var path = require('path');
var fs = require('fs');

var myconfig;
fs.readFile('./myconfig.json', 'utf8', function (err, data) {
if (err) {
console.log("ERROR: Configuration load - " + err);
throw err;
} else {
try {
myconfig = JSON.parse(data);
console.log("Configuration loaded successfully");
}
catch (ex) {
console.log("ERROR: Configuration parse - " + err);
}


}
});


I get the following error when I run this:



SyntaxError: Unexpected token ´╗┐
at Object.parse (native)
...


Now, when I change the file encoding (using Notepad++) to ANSI, it works without a problem.


Any ideas why this is the case? Whilst development is being done on Windows the final solution will be deployed to a variety of non-Windows servers, I'm worried that I'll run into issues on the server end if I deploy an ANSI file to Linux, for example.


According to my searches here and via Google the code should work on Windows as I am specifically telling it to expect a UTF-8 file.



asked 46 secs ago






Aucun commentaire:

Enregistrer un commentaire