lambda-local icon indicating copy to clipboard operation
lambda-local copied to clipboard

Supporting ES modules as Lambda functions

Open shaunhurley opened this issue 2 years ago • 1 comments

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
}

image

shaunhurley avatar May 06 '22 17:05 shaunhurley

Just noticed that AWS has made Node 16 generally available, which makes this even more timely, no?

shaunhurley avatar May 14 '22 00:05 shaunhurley

Any progress on this?

Tomas2D avatar Oct 20 '22 11:10 Tomas2D

AWS is now supporting Node 18 as well....

shaunhurley avatar Feb 18 '23 23:02 shaunhurley

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.

gpotter2 avatar Mar 04 '23 18:03 gpotter2

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 :(

shaunhurley avatar Mar 04 '23 18:03 shaunhurley

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)

tdanecker avatar Jul 19 '23 10:07 tdanecker

Closed in https://github.com/ashiina/lambda-local/pull/230. Thanks again @tdanecker. Released in 2.1.0

gpotter2 avatar Jul 19 '23 23:07 gpotter2