I am trying this in the AWS lambda console. I have installed npm install @aws-sdk/client-kinesis on my terminal and used zipped the file and created a lambda layer which has client-kinesis.
If use the following it works!
// ES5 example
const { KinesisClient, AddTagsToStreamCommand } = require("@aws-sdk/client-kinesis");
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
If I am using the following it is giving me errors-
//ES6+ example
import { KinesisClient, AddTagsToStreamCommand } from "@aws-sdk/client-kinesis";
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
"Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",
Question -
Thanks !
In order to fix the error, you could opt to use at least NodeJS version 18 or the recommended version NodeJS 20 as your Lambda function runtime and add "type": "module"
to your package.json file. This will inform the Node runtime to use ES6 modules instead of the traditional ES5/CommonJS syntax.
or consider changing the extension of your files to .mjs
to force the NodeJS runtime to treat your file as an ES6 module.
If you are using esbuild
, you will have to make sure that your build output is compatible with the ES6 modules (import/export).
If you were using TypeScript, configure your transpiler to produce a build output that is compatible with ES modules.
If there is a performance issue, it would be minimal that we don't have to worry about it! PS: I recommend to test the compatibility of the ES5/CJS/CommonJS modules that the lambda uses when you change the runtime.