lambda-local
lambda-local copied to clipboard
Supporting ES modules as Lambda functions
Hi, I use lambda-local extensively, kudos for a super useful utility!
With the availability of Node 14.x, AWS is supporting ES Modules as lambda functions (package.json contains the "type":"module"
directive) enabling the use of import instead of require in the code base.
Unless I'm missing something, it doesn't appear that lambda-local supports this format yet? I was able to get a skeleton function working as expected in Lambda, however in lambda-local the same code throws an error
"require() of ES Module <blah blah>\\test-es6-module\\index.js from <blah blah>\\lambda-local\\build\\lambdalocal.js not supported.\nInstead change the require of index.js in <blah blah>\\lambda-local\\build\\lambdalocal.js to a dynamic import() which is available in all CommonJS modules."
Working code/result from Lambda:
{
"name": "test-es6-module",
"version": "1.0.0",
"description": "Testing es6 modules",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
import querystring from "querystring";
const handler = async (event, context) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
console.log('value2 =', event.key2);
console.log('value3 =', event.key3);
return event.key1; // Echo back the first key value
// throw new Error('Something went wrong');
};
export {
handler
}
Just noticed that AWS has made Node 16 generally available, which makes this even more timely, no?
Any progress on this?
AWS is now supporting Node 18 as well....
Feel free to contribute by submitting a PR. It's not really useful to just remind me that it would be nice to find the time to do that.
Feel free to contribute by submitting a PR. It's not really useful to just remind me that it would be nice to find the time to do that.
I do understand the time constraints, and I wish I could :( I did try to get something working locally to figure it out, however I'm afraid my JavaScript / NodeJS skills are not up to this particular task, or I absolutely would have :(
I can provide a pull request for ES module support, but there are two caveats:
- watch mode will not be supported for ES modules as the loader cache can't be invalidated (https://github.com/nodejs/node/issues/49442)
- to keep downward compatibility with NodeJS < 16, the dynamic import for the ES module has to be wrapped in an eval statement (https://github.com/microsoft/TypeScript/issues/43329#issuecomment-1008361973)
Closed in https://github.com/ashiina/lambda-local/pull/230. Thanks again @tdanecker. Released in 2.1.0