问题
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