Skip to content

ES Module support #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
edeesis opened this issue Mar 1, 2022 · 6 comments
Closed

ES Module support #266

edeesis opened this issue Mar 1, 2022 · 6 comments

Comments

@edeesis
Copy link

edeesis commented Mar 1, 2022

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)"
  ]
}
@astuyve astuyve self-assigned this Mar 1, 2022
@DarcyRaynerDD
Copy link
Contributor

Hi @edeesis. Thanks for the report. We've scheduled work to support this with our js client. Unfortunately it's not quite as simple as updating the UserFunction.js, since the new code depends on a native library called deasync to support top level await. This means we must publish separate layers for ARM64 and x86 architectures, and update some of our other tooling as well. We will update this issue with our progress.

@astuyve astuyve removed their assignment Mar 1, 2022
@andcea
Copy link

andcea commented Mar 6, 2022

I enabled ESM on my existing (and working) NodejsFunction using the new config bundling: { format: lambdaNodeJs.OutputFormat.ESM } and I'm getting the following error:

{
    "errorType": "Runtime.HandlerNotFound",
    "errorMessage": "index.post is undefined or not exported",
    "stack": [
        "Runtime.HandlerNotFound: index.post is undefined or not exported",
        "    at HandlerNotFound.ExtendedError [as constructor] (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:113:28)",
        "    at new HandlerNotFound (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:131:42)",
        "    at load (/opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js:151:15)",
        "    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)",
        "    at require (internal/modules/cjs/helpers.js:93:18)"
    ]
}

I assume it's related to this issue and lack of support?

@astuyve astuyve mentioned this issue Apr 21, 2022
11 tasks
@astuyve
Copy link
Contributor

astuyve commented May 17, 2022

ESM support is released in Layer version 78 and version 6.78 of this library!

@astuyve astuyve closed this as completed May 17, 2022
@edeesis
Copy link
Author

edeesis commented May 17, 2022

Thanks @astuyve! Fantastic news, and happy for the relatively quick turn around given the complexity of the change required.

@paco-sparta
Copy link

Just a bit more work to get this working :D #295

@beeequeue
Copy link

We tried to add the DD layer to our ESM built lambda but found that if we do the lambda cannot import dd-trace anymore. It is already marked as external and works with CJS but not with ESM. 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants