Node js Syntax error unexpected token?

1.3k Views Asked by At

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

There are 1 best solutions below

3
On BEST ANSWER

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: