Node.js: module not found using ES import with Lambda layers
We're trying to deploy a Lambda function with AWS SDK v3 modules. The imports are standard ES syntax and work fine in a local setup, and work if we package the entire module into the function:
import { SESv2Client, SendEmailCommand } from '@aws-sdk/client-sesv2';
For efficiency and reuse, we moved the modules into a layer. When we run the function (tried with Node.js 14 and 16 runtimes), we continue to receive:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@aws-sdk/client-sesv2' imported from /var/task/index.js
If we strip out the ES pieces and use CommonJS syntax with require
statements, the modules load correctly. Obviously we would prefer the modern syntax and the performance gains that go with it. According to AWS docs, node.js lambda functions support ES imports in Node.js 14.x and Node.js 16.x.
Has anyone else run into this and found a solution?
Unfortunately AWS support has confirmed we can't utilize layers with ES:
importing ESM modules from layers seems to not be supported when using a module type in Lambda. [...] this is a Node feature blocking us to import ES modules from a layer as "NODE_PATH" variable is not supported by the ESM loader in node
Disappointing that AWS is making headway on supporting ES, but it means we lose the functionality of layers. Support provided a wrapper workaround that we'll be trying. It corresponds with what we found here. Hopefully this saves someone else the headache we've gone through the last week.
Relevant questions
aws-sdk V3 timeout in lambda
Accepted Answerasked 6 months agoaws lambda - ES6 module error : module is not defined in ES module scope
Accepted Answerasked 6 months agoDeploy Lambda Function and API Gateway REST
asked 4 months agoPython os module doesn't work in lambda execution
asked 3 years agoNode.js: module not found using ES import with Lambda layers
Accepted Answerasked 13 days agoError when trying to import package installed using lambda layer on nodejs14 ES module enabled lambda function
asked 5 months agoProfiling memory for a NodeJS Lambda function
asked 6 months agoHow to use AWS CDK to compile and deploy a typescript api with dependencies to lambda?
asked 5 months agoUsing the encryption sdk within lambda
Accepted Answerasked 2 years agoGreengrass v2 interact with shadows from a nodejs lambda
Accepted Answerasked 18 days ago