AWS Lambda Error – SyntaxError: Unexpected token

 

AWS Lambda Error – SyntaxError: Unexpected token

A practical diagnostic guide for resolving syntax parsing failures in Node.js Lambdas caused by invalid JSON, malformed code, packaging mistakes, or hidden Unicode characters.





Problem

Your Lambda fails immediately with errors such as:

SyntaxError: Unexpected token < in JSON at position 0

or:

SyntaxError: Unexpected token '.'

or:

SyntaxError: Unexpected token in JSON at position X

This means Node.js failed to parse either your code or the JSON data you're handling.

Possible causes:

  • Malformed JSON input
  • HTML or error pages returned instead of JSON
  • ES module syntax used in a CommonJS runtime
  • Hidden characters introduced during packaging
  • Uploading the wrong file as the Lambda handler

Clarifying the Issue

There are two failure categories:

  • Code cannot be parsed – Node.js halts before the function starts.
  • Data cannot be parsed – Handler crashes while processing input.

Node.js enforces strict syntax rules; Lambda does not modify them.


Why It Matters

Syntax failures:

  • Prevent the handler from ever executing
  • Cause wasted retries
  • Destroy debugging visibility
  • Break upstream API flows

You must resolve syntax issues before investigating application logic.


Key Terms

  • SyntaxError – JavaScript runtime error when parsing fails.
  • JSON.parse() – Converts JSON strings to objects; throws on invalid JSON.
  • ESM vs. CommonJS – import vs. require module systems.

Steps at a Glance

  1. Check CloudWatch for the exact token
  2. Validate event input JSON
  3. Detect HTML instead of JSON
  4. Check code syntax locally
  5. Confirm module system compatibility
  6. Scan for hidden characters
  7. Re-test with a clean deployment

Detailed Steps

Step 1: Check CloudWatch for the exact offending token

Run:

aws logs tail /aws/lambda/my-function --since 5m

Identify the specific token (<{".). Now validate incoming JSON.


Step 2: Validate event input JSON

Add:

console.log("RAW EVENT:", JSON.stringify(event));

If parsing a string:

console.log("TO PARSE:", body);

Validate using jq:

echo "$body" | jq .

If JSON is valid, check for HTML.


Step 3: Detect HTML returned instead of JSON

If you see:

Unexpected token <

The payload starts with <, meaning HTML.

Log the response:

console.log("RESPONSE:", response.data);

If HTML appears, fix the upstream endpoint.


Step 4: Check code syntax locally

Run:

node index.js

Common failures:

  • Trailing commas
  • Missing braces
  • Hidden Unicode characters
  • ES syntax newer than runtime supports

Fix locally before deploying.


Step 5: Confirm module system compatibility

CommonJS:

const x = require("./x");

ES Modules:

import x from "./x.js";

If package.json includes:

{ "type": "module" }

Node expects ESM. Mismatch = SyntaxError.


Step 6: Scan for hidden or corrupted characters

Detect with:

cat -A index.js

If weird sequences (M-BM, etc.) appear, clean the file.


Step 7: Re-test with a clean deployment

Deploy:

zip -r function.zip .
aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip

Invoke:

aws lambda invoke --function-name my-function --payload '{}' out.json

If successful, the syntax issue is resolved.


Pro Tips

  • Validate JSON before calling JSON.parse()
  • Log full API responses before parsing
  • Use ESLint to catch syntax errors pre-deploy

Conclusion

“Unexpected token” errors always come from malformed code or malformed data. By validating JSON, checking syntax locally, verifying module formats, and removing hidden characters, you can restore stable execution and eliminate parsing failures in Lambda.


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Insight: The Great Minimal OS Showdown—DietPi vs Raspberry Pi OS Lite

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison