Node js Syntax error unexpected token?

女生的网名这么多〃 提交于 2019-12-11 08:14:00

问题


Everything in my code and log4js file seems to be fine and syntactically correct. However, when running my app, I get this following error:

undefined:1
?{
^
SyntaxError: Unexpected token ?

It happens when I get to this line:

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

This is my log4js file

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

I have no idea why it might think there is a "?" character in the beginning, the log4js file has no syntax errors.


回答1:


You may have a BOM character or some other unprintable character there which is illegal in JSON. Alternatively you may be using a character encoding other than one of the only 5 legal encodings of JSON which are UTF-8, UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE.

For more details on why BOM is illegal in JSON see those answer:

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


来源:https://stackoverflow.com/questions/41346687/node-js-syntax-error-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!