node.js readfile error with utf8 encoded file on windows

后端 未结 2 1308
孤街浪徒
孤街浪徒 2020-12-03 13:30

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         


        
相关标签:
2条回答
  • 2020-12-03 14:06

    Per "fs.readFileSync(filename, 'utf8') doesn't strip BOM markers #1918", fs.readFile is working as designed: BOM is not stripped from the header of the UTF-8 file, if it exists. It at the discretion of the developer to handle this.

    Possible workarounds:

    • data = data.replace(/^\uFEFF/, ''); per https://github.com/joyent/node/issues/1918#issuecomment-2480359
    • Transform the incoming stream to remove the BOM header with the NPM module bomstrip per https://github.com/joyent/node/issues/1918#issuecomment-38491548

    What you are getting is the byte order mark header (BOM) of the UTF-8 file. When JSON.parse sees this, it gives an syntax error (read: "unexpected character" error). You must strip the byte order mark from the file before passing it to JSON.parse:

    fs.readFile('./myconfig.json', 'utf8', function (err, data) {
        myconfig = JSON.parse(data.toString('utf8').replace(/^\uFEFF/, ''));
    });
    // note: data is an instance of Buffer
    
    0 讨论(0)
  • 2020-12-03 14:17

    To get this to work without I had to change the encoding from "UTF-8" to "UTF-8 without BOM" using Notepad++ (I assume any decent text editor - not Notepad - has the ability to choose this encoding type).

    This solution meant that the deployment guys could deploy to Unix without a hassle, and I could develop without errors during the reading of the file.

    In terms of reading the file, the other response I sometimes got in my travels was a question mark appended before the start of the file contents, when trying various encoding options. Naturally with a question mark or ANSI characters appended the JSON.parse fails.

    Hope this helps someone!

    0 讨论(0)
提交回复
热议问题