SyntaxError: Unexpected token { in JSON at position 2955 - AWS Lambda NodeJS module of APM

Hello,

We are trying to implement the AWS Lambda agent for APM as per below documentation:

Added below variables:

Defined outbound rules for security groups under 'VPC' section over port 8200

However after firing some requests we are getting this error in Cloudwatch:

{"log.level":"debug","@timestamp":"2022-10-12T14:25:05.451Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"error":{"type":"UserCodeSyntaxError","message":"SyntaxError: Unexpected token { in JSON at position 2955","stack_trace":"Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token { in JSON at position 2955\n at _loadUserApp (/var/runtime/UserFunction.js:218:13)\n at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)\n at Object.<anonymous> (/var/runtime/index.js:43:34)\n at Module._compile (internal/modules/cjs/loader.js:1085:14)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)\n at Module.load (internal/modules/cjs/loader.js:950:32)\n at Function.Module._load (internal/modules/cjs/loader.js:790:12)\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)\n at internal/main/run_main_module.js:17:47"},"message":"Elastic APM caught unhandled exception"}

We have selected node v14.20.0 for Lambda, but we are still having this error. Trace log of the APM module:

{
    "log.level": "trace",
    "@timestamp": "2022-10-12T14:25:03.563Z",
    "log": {
        "logger": "elastic-apm-node"
    },
    "pid": 17,
    "ppid": 1,
    "arch": "x64",
    "platform": "linux",
    "node": "v14.20.0",
    "agent": "3.38.0",
    "startTrace": [
        "at Agent.start (/opt/nodejs/node_modules/elastic-apm-node/lib/agent.js:241:11)",
        "at Object.<anonymous> (/opt/nodejs/node_modules/elastic-apm-node/start.js:9:32)",
        "at Module._compile (internal/modules/cjs/loader.js:1085:14)",
        "at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
        "at Module.load (internal/modules/cjs/loader.js:950:32)",
        "at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
        "at Module.require (internal/modules/cjs/loader.js:974:19)",
        "at Module._preloadModules (internal/modules/cjs/loader.js:1244:12)",
        "at loadPreloadModules (internal/bootstrap/pre_execution.js:475:5)",
        "at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:72:3)"
    ],
    "main": "<could not determine>",
    "dependencies": "<could not determine>",
    "conf": {
        "ignoreUrlStr": [],
        "ignoreUrlRegExp": [],
        "ignoreUserAgentStr": [],
        "ignoreUserAgentRegExp": [],
        "transactionIgnoreUrlRegExp": [],
        "sanitizeFieldNamesRegExp": [
            "/^password$/i",
            "/^passwd$/i",
            "/^pwd$/i",
            "/^secret$/i",
            "/^.*key$/i",
            "/^.*token.*$/i",
            "/^.*session.*$/i",
            "/^.*credit.*$/i",
            "/^.*card.*$/i",
            "/^.*auth.*$/i",
            "/^set\\x2dcookie$/i",
            "/^pw$/i",
            "/^pass$/i",
            "/^connect\\.sid$/i"
        ],
        "ignoreMessageQueuesRegExp": [],
        "abortedErrorThreshold": 25,
        "active": true,
        "apiRequestSize": 786432,
        "apiRequestTime": 10,
        "breakdownMetrics": false,
        "captureBody": "off",
        "captureErrorLogStackTraces": "messages",
        "captureExceptions": true,
        "captureHeaders": true,
        "centralConfig": false,
        "cloudProvider": "none",
        "contextPropagationOnly": false,
        "disableInstrumentations": [],
        "disableSend": false,
        "environment": "production",
        "errorOnAbortedRequests": false,
        "exitSpanMinDuration": 0,
        "filterHttpHeaders": true,
        "ignoreMessageQueues": [],
        "instrument": true,
        "instrumentIncomingHTTPRequests": true,
        "logLevel": "trace",
        "logUncaughtExceptions": false,
        "longFieldMaxLength": 10000,
        "maxQueueSize": 1024,
        "metricsInterval": 0,
        "metricsLimit": 1000,
        "opentelemetryBridgeEnabled": false,
        "sanitizeFieldNames": [
            "password",
            "passwd",
            "pwd",
            "secret",
            "*key",
            "*token*",
            "*session*",
            "*credit*",
            "*card*",
            "*auth*",
            "set-cookie",
            "pw",
            "pass",
            "connect.sid"
        ],
        "serverTimeout": 30,
        "sourceLinesErrorAppFrames": 5,
        "sourceLinesErrorLibraryFrames": 5,
        "sourceLinesSpanAppFrames": 0,
        "sourceLinesSpanLibraryFrames": 0,
        "spanCompressionEnabled": true,
        "spanCompressionExactMatchMaxDuration": 0.05,
        "spanCompressionSameKindMaxDuration": 0,
        "stackTraceLimit": 50,
        "traceContinuationStrategy": "continue",
        "transactionIgnoreUrls": [],
        "transactionMaxSpans": 500,
        "transactionSampleRate": 1,
        "useElasticTraceparentHeader": true,
        "usePathAsTransactionName": false,
        "verifyServerCert": true,
        "secretToken": "[REDACTED]",
        "serviceName": "[REDACTED]",
        "serviceVersion": "$LATEST",
        "spanStackTraceMinDuration": -1,
        "serverHost": "[REDACTED]",
        "serverPort": 8200
    },
    "ecs": {
        "version": "1.6.0"
    },
    "message": "agent configured correctly"
}

Could you please help me in this matter ?

Hello @preetish_P,

That log statement from the APM agent is saying that it has captured an "uncaughtException" that was raised by the Node.js program. The APM agent will attempt to report the exception details to APM server and then either pass the exception on to other registered uncaughtException handlers, or terminate the process.

The exception it caught has this stack trace:

Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token { in JSON at position 2955
 at _loadUserApp (/var/runtime/UserFunction.js:218:13)
 at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)
 at Object.<anonymous> (/var/runtime/index.js:43:34)
 at Module._compile (internal/modules/cjs/loader.js:1085:14)
 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
 at Module.load (internal/modules/cjs/loader.js:950:32)
 at Function.Module._load (internal/modules/cjs/loader.js:790:12)
 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
 at internal/main/run_main_module.js:17:47

So that looks like it was an exception in the AWS Lambda Runtime code. Looking at the Lambda Runtime code (from GitHub - aws/aws-lambda-base-images at nodejs14.x which currently has version "14.20.1-633b2f83", slightly different that your 14.20.0) the exception is in this code:

/**
 * Load the user's application or throw a descriptive error.
 * @throws Runtime errors in two cases
 *   1 - UserCodeSyntaxError if there's a syntax error while loading the module
 *   2 - ImportModuleError if the module cannot be found
 */
function _loadUserApp(appRoot, moduleRoot, module) {
  if (!NoGlobalAwsLambda) {
    globalThis.awslambda = {
      streamifyResponse: (handler, options) => {
        handler[HANDLER_STREAMING] = STREAM_RESPONSE;
        if (typeof options?.highWaterMark === "number") {
          handler[HANDLER_HIGHWATERMARK] = parseInt(options.highWaterMark);
        }
        return handler;
      },
      HttpResponseStream: HttpResponseStream,
    };
  }

  try {
    return _tryRequire(appRoot, moduleRoot, module);
  } catch (e) {
    if (e instanceof SyntaxError) {
      throw new UserCodeSyntaxError(e);
    } else if (e.code !== undefined && e.code === "MODULE_NOT_FOUND") {
      verbose("globalPaths", () => JSON.stringify(require("module").globalPaths));
      throw new ImportModuleError(e);
    } else {
      throw e;
    }
  }
}

My guess is that the Lambda Runtime is attempting to require(...) your handler file, but it is hitting syntax that it (Node v14.20.0) doesn't understand.

  • Are you able to attempt to require(...) your handler module in a dev environment using node v14.20.0 to attempt to reproduce the error?
  • The error message "Unexpected token { in JSON at position 2955" suggests there is some JSON file that is being loaded. Do you have some .json files that your handler code is attempting to require(...) or import ...?
  • The AWS Lambda Runtime has an environment variable to turn on some verbose logging that might provide some more details: set AWS_LAMBDA_RUNTIME_VERBOSE=1 for "verbose", or AWS_LAMBDA_RUNTIME_VERBOSE=2 for "vverbose", or =3 for "vvverbose".

SyntaxError: Unexpected token in JavaScript #
The "Uncaught SyntaxError: Unexpected token" occurs for multiple reasons:

Having a Make sure that your script tags point to a valid path and try renaming all your files to lowercase letters only. Sometimes the error is caused if the file name contains uppercase letters or special characters.

Another common cause of the error is forgetting to close a script tag.

index.html

<!--  Forgot to close the script tag  -->
<script
  console.log("Uncaught SyntaxError: Unexpected token '<'");
</script>
The error is also caused if you're making an HTTP request to a server and getting back an HTML response when you're trying to parse JSON data.

To resolve this, console.log the response you're getting from your server and make sure it's a valid JSON string and does not contain any HTML tags.

You can also open the developer tools of your browser, click on theNetwork tab and inspect the response.
The "Uncaught SyntaxError: Unexpected token" error also occurs if you have a missing or extra bracket, parenthesis or comma.

index.js
// SyntaxError: Unexpected token '}'
function sum(a, b) {
return a + b;
}}
There is an extra curly brace on the third line, which causes the error.

unexpected token curly brace

You can paste your code into an online Syntax Validator. The validator should be able to tell you on which line the error occurs.

Alternatively, you can also open your browser's console and see the line number. It will look something like index.js:4. This means that the error occurred in the index.js file on line 4.
These syntax errors are very tricky to find, but a general rule of thumb is:

if you are getting the "Uncaught SyntaxError: Unexpected token '<'" (notice the <), you are probably trying to read some HTML code that starts with <.
if your error message contains a curly brace, parenthesis, comma, colon, etc, you most likely have a SyntaxError, where you have an extra or a missing character in your code.
The error message means that one character was expected, but another character was provided. This is commonly due to typos.
Here's another example.

index.js
// Uncaught SyntaxError: Unexpected token ':'
const obj = {
name:: "Tom",
}
We separated the key and value in the object with 2 colons instead of 1, which caused the error.

This could also occur if you have an extra comma.

index.js
// Uncaught SyntaxError: Unexpected token ','
const obj = {
name: 'Tom',,
}
However, the brackets and parentheses are the most difficult SyntaxErrors to track down.

Conclusion #
To solve the "Uncaught SyntaxError: Unexpected token" error, make sure:

You don't have a tag that points to an HTML file, instead of a JS file.
You aren't requesting an HTML file from a server, instead of requesting JSON.
You don't have a tag that points to an incorrect path.
You don't have missing or extra brackets, parentheses or commas in your code.
You haven't forgotten to close a <script tag.

This may help you,
Rachel Gomez

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.