1

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.

Daniel
  • 601
  • 8
  • 18
  • Did you file contain UTF-8 BOM symbol? – vitalyster Dec 27 '16 at 14:08
  • Yes, my file starts with it. Is it not supposed to contain it? EDIT: ok... I just compared the file to another json file and its not supposed to have it, I removed the symbol and now everything is working. Thank you very much for bringing this to my attention. Do you happen to know why this symbol was added to my file when i created it? – Daniel Dec 27 '16 at 14:10

1 Answers1

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:

Community
  • 1
  • 1
rsp
  • 91,898
  • 19
  • 176
  • 156
  • Yup I did have a BOM character in the start of my JSON file, I removed it and it works fine now. What I do want to know though is why the BOM character was added to the start of my JSON file since I know I didn't manually add it. Have any idea? – Daniel Dec 27 '16 at 14:43
  • @Daniel It's hard to say. Maybe it's your text editor or some other tool that processed or saved the JSON has added the BOM character. Some tools are written under the assumption that the BOM character is harmless and add them without notice and it causes problems, one of which is with JSON which explicitly states in [RFC 7159, Section 8.1](https://tools.ietf.org/html/rfc7159#section-8.1) on Character Encoding: "Implementations MUST NOT add a byte order mark to the beginning of a JSON text." This is the only "MUST NOT" in the entire RFC so it's pretty important. – rsp Dec 27 '16 at 15:21
  • yup i took a look at the answers you linked and saw the RFC reference. Well I learned something new and that's helpful. Thank you :) – Daniel Dec 27 '16 at 15:40