Skip to content

ES Module support #266

Closed
Closed
@edeesis

Description

@edeesis

Expected Behavior

According to this issue, Lambda has updated the latest version of the amazon/aws-lambda-nodejs image to support the top level await by adding the following code:

const loaded =
    (pjHasModule && _tryAwaitImport(lambdaStylePath, ".js")) ||
    _tryAwaitImport(lambdaStylePath, ".mjs") ||
    _tryRequireFile(lambdaStylePath, ".cjs");

Since this repo now has a vendored version of UserFunction.js, this code should be updated to work appropriately.

Actual Behavior

An error is raised on start up when attempting to load a ES Module file. See stacktrace below.

Steps to Reproduce the Problem

  1. Use a ES Module file as your lambda handler (.mjs file should work)
  2. Declare that as the DD_LAMBDA_HANDLER environment variable

Specifications

  • Datadog Lambda Layer version: 4.65.0+
    • Since 4.64.0 has no vendored version
  • Node version: 14.x

Stacktrace

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js\n- /opt/nodejs/node_modules/datadog-lambda-js/runtime/index.js\n- /opt/nodejs/node_modules/datadog-lambda-js/handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "stack": [
      "Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
      "Require stack:",
      "- /opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js",
      "- /opt/nodejs/node_modules/datadog-lambda-js/runtime/index.js",
      "- /opt/nodejs/node_modules/datadog-lambda-js/handler.js",
      "- /var/runtime/UserFunction.js",
      "- /var/runtime/index.js",
      "    at ImportModuleError.ExtendedError [as constructor] (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:113:28)",
      "    at new ImportModuleError (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:123:42)",
      "    at _loadUserApp (/opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js:113:19)",
      "    at load (/opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js:148:19)",
      "    at Object.<anonymous> (/opt/nodejs/node_modules/datadog-lambda-js/handler.js:65:59)",
      "    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)"
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions