Node.js — SyntaxError: Unexpected token caught at startup
Locate syntax errors quickly by reading the reported position and isolating the file from the build output.
Symptoms
- SyntaxError: Unexpected token < in JSON at position 0
- SyntaxError: Unexpected token }
- The error appears only after a build or deploy
- The failing file runs in the editor but not under node
Possible causes
- The file is HTML or an error page parsed as JavaScript or JSON
- An unbalanced brace, bracket or quote in the source
- A build artifact is stale or partially written
- A JSON response returns an HTML login page
Diagnosis steps
Read the full error with the stack
node --stack-trace-limit=20 file.jsInterpretation
The frame above the error names the file and line, which is often enough to see the offending token.
Validate JSON separately when relevant
node -e "JSON.parse(require('fs').readFileSync('config.json','utf8'))"Interpretation
Unexpected token < at position 0 almost always means HTML was parsed as JSON, such as a redirected request.
Rebuild to replace stale artifacts
rm -rf dist && npm run buildInterpretation
A half-written or outdated bundle can contain invalid syntax that no longer exists in the source.
Root cause
The runtime parsed text that was not valid JavaScript or JSON at the reported position, either because the source has a syntax error or because non-code content was parsed as code.
Fix
Fix the syntax error in the source, validate JSON before parsing, and rebuild so artifacts reflect the current source. For Unexpected token <, treat the response as an error page rather than data.
Verify the fix
Run the file again and confirm it loads, and rerun the parse on the actual data to confirm the content is now valid.
Symptom walkthrough
Node reports SyntaxError: Unexpected token with a position or a token that does not belong, and the file may have run fine in a tool with looser parsing. When the message is Unexpected token < in JSON at position 0, the parser almost certainly received an HTML page rather than JSON, often because an authenticated API returned a login screen.
Fix in practice
Read the reported line and fix the actual syntax error, or, for JSON failures, validate the response and treat non-JSON as an error rather than data. When the error only appears after a build, rebuild from clean to rule out stale artifacts that no longer match source.
Verification
Run the file again and confirm it loads without the syntax error, and parse the real payload to confirm the content is valid. Both checks together prove the parse path is healthy.
Diagnostic commands
Check the behavior with commands like these:
node --stack-trace-limit=20 file.js
node -e "JSON.parse(require('fs').readFileSync('config.json','utf8'))"
rm -rf dist && npm run build