Dave Meehan
Dave Meehan

Reputation: 3201

Using ES modules in AWS Lambda created by Amplify CLI

According to the AWS blog, use of ES modules in Lambda is supported as of the Nodejs14 runtime.

Announcement - https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/

Example - https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

I have checked that the Lambda function runtime is Node14 (and tried switching to Node18 without any difference) - I checked via the Lambda console once I'd pushed the code, and checked that the version changes to 18 when the setting in the Amplify config is changed.

I won't go into the detail of how I got here, other than I need to use an npm package that is written to ESM syntax.

As a sanity check and as a minimum reproducable example, I generated a new simple hello world function with the Amplify CLI, and then ran it with amplify mock function test --event src/event.json and confirmed it runs ok. But when I change the package.json to "type":"module" I get:

  stack: 'Error: Could not load lambda handler function due to Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /[redacted]/amplify/backend/function/test/src/index.js\n' +
    'require() of ES modules is not supported.\n' +
    'require() of /[redacted]/amplify/backend/function/test/src/index.js from /snapshot/repo/build/node_modules/amplify-nodejs-function-runtime-provider/lib/utils/execute.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.\n' 

I get this same error whether I exercise the function from the amplify mock function CLI, the Lambda console, or by accessing the API gateway that links to the Lambda function.

Beyond the link blog posts above, I can't find any other mention or examples of using ES Modules with Lambda.

If you want to do this yourself:

  1. Install amplify cli
  2. amplify init
  3. amplify add function and name it test, choose Nodejs, Hello World template
  4. amplify mock function test --event src/event.json and it will work
  5. Change amplify/backend/function/test/src/package.jsonto include"type":"module"`
  6. amplify mock function test --event src/event.json and it will fail

Optionally you can push the application to AWS and test the lambda through the lambda console, you should get the same results.

Related issues:

Upvotes: 3

Views: 1324

Answers (1)

MartinS
MartinS

Reputation: 126

There are two different issues described here. First one is with 'libphonenumebr-js' that accroding to the github ticket was fixed in the latest version of the package.

Second issues is that amplify mock does not support ES import which is really a shame. Same way it does not support lambda layers if they are enabled. So you need to deploy to aws first and then test your changes there.

In lambda console 'require() of ES modules is not supported.\n' error would happen if package.json is not set to type module.

Upvotes: 0

Related Questions