在Windows上使用utf8编码的文件导致node.js读取文件错误 [英] node.js readfile error with utf8 encoded file on windows

查看:210
本文介绍了在Windows上使用utf8编码的文件导致node.js读取文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows 8.1上使用node.js(0.10.29)从磁盘加载一个UTF8 json文件。以下是运行的代码:

  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);
}


}
});

当我运行这个命令时,出现以下错误:

  SyntaxError:在Object.parse(本地)
...
意外的令牌'

现在,当我将文件编码(使用Notepad ++)更改为ANSI时,它可以毫无问题地使用。



任何想法,为什么这样?在Windows上进行开发时,最终的解决方案将被部署到各种非Windows服务器上,例如,如果我将ANSI文件部署到Linux上,我担心服务器端出现问题。 / p>

根据我在这里和Google的搜索,代码应该在Windows上工作,因为我明确地告诉它需要一个UTF-8文件。



示例配置我读:

  {
ListenIP4:10.10.1.1 ,
ListenPort:8080
}


解决方案<每个fs.readFileSync(filename,'utf8')不剥离BOM标记#1918 a>, fs.readFile 按设计工作:不是从UTF-8文件的头部剥离



可能的解决方法:





你得到的是UTF-8文件的字节序标记头(BOM)。当 JSON.parse 看到这个时,它会给出一个语法错误(读取:意外字符错误)。在传递给 JSON.parse 之前,必须从文件中去掉字节顺序标记:

  fs.readFile('./ myconfig.json','utf8',function(err,data){
myconfig = JSON.parse(data.toString('utf8')。replace(/ ^ \\\ /,''));
});
//注意:data是Buffer的一个实例


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.

Sample config I am reading:

{
    "ListenIP4": "10.10.1.1",
    "ListenPort": 8080
}

解决方案

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:

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

这篇关于在Windows上使用utf8编码的文件导致node.js读取文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆