Im building my first Lambda function, and wants to do it locally. So I have naturally installed SAM to do so.
From what I understand, AWS recently pushed a new version that would allow ES6 syntax with import/export and so on. But this is not the case for me, when I try to 'import' at the top of my handler-file, I'm getting this error:
sam local invoke -e ./lambda/lambda_event.json LambdaDemoFunction
:
Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.37.0-x86_64.
Mounting /Users/oakleaf/Work/Git/LCMSLambdaDispatcher/Lambda as /var/task:ro,delegated inside runtime container
START RequestId: d222a7f3-580f-4464-80fb-88a373d2ea09 Version: $LATEST
2022-02-14T21:51:58.695Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:200:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," 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 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)"," at internal/main/run_main_module.js:17:47"]}
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Function 'LambdaDemoFunction' timed out after 3 seconds
No response from invoke container for LambdaDemoFunction
My template.yaml in my root:
LambdaDemoFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: Lambda/ # format is projectPath/
Handler: lambda.lambdaHandler #format is filename.functionName
Runtime: nodejs14.x
and then, inside /lambda/sdk I have a file called "Dispatcher.mjs" (Have also tried with "Dispatcher.js")
that contains:
export default class Dispatcher
{
constructor()
{
}
echo()
{
return "HEj hej från dispatcher";
}
}
And lastly, my handler-file:
import { Dispatcher } from "./sdk/Dispatcher";
exports.lambdaHandler = async event => {
return "hola";
};
I also have a package.json-file but is it even used right now? Im not sure, since I have not used npm or anything. It contains (among other things) "type": "module".
Any idea what I can do here to be able to use import/export properly, es6 style, in my Lambda?
From the docs: https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/