节点 js 语法错误意外令牌?

Node js Syntax error unexpected token?

我的代码和 log4js 文件中的所有内容似乎都很好并且语法正确。但是,当 运行 我的应用程序时,出现以下错误:

undefined:1
?{
^
SyntaxError: Unexpected token ?

当我到达这一行时发生:

var logFile = 'log4js_' + process.env.NODE_ENV + '.json';
log4js.configure(logFile);

这是我的 log4js 文件

{
    "appenders": [
    {
      "type": "console"
    },
    {
      "type": "file",
      "filename": "logs/main.log",
      "maxLogSize": 1024000,
      "category": "main"
    }
    ]
}

我不知道为什么它会认为有一个“?”字符开头,log4js文件没有语法错误。

您可能有 BOM 字符或其他一些在 JSON 中是非法的不可打印字符。或者,您可能使用的字符编码不是 JSON 仅有的 5 种合法编码之一,即 UTF-8、UTF-16BE、UTF-16LE、UTF-32BE 或 UTF-32LE。

有关为什么 BOM 在 JSON 中非法的更多详细信息,请参阅这些答案:

  • JSON Specification and usage of BOM/charset-encoding
  • What's different between UTF-8 and UTF-8 without BOM?