I am using the AWS CDK (v2.140.0
) to deploy a single lambda function written in ES6 javascript and with a Node v20.x
runtime. The root of the project contains a package.json
file in which I am defining "type": "module"
. I am using aws CDK and SDK packages but otherwise am not consuming any 3rd party libraries in my application code.
My file structure looks like:
bin
cdk.js
lib
my-stack.js
src
index.js
package.json
cdk.json
I am able to successfully synth and deploy the lambda to AWS. The lambda runs on an Eventbridge schedule and is invoked at the correct times.
However, I am getting the following error in Cloudwatch: SyntaxError: Cannot use import statement outside a module
My index.js
file is written as:
import { foo } from './foo-service.js';
export const handler = async () => {
// do stuff with foo
};
I have gone down a few rabbit holes and tried most of the suggestions from this SO post without any success.
- changing my
index.js
file to aindex.mjs
- using the “node” specific CDK construct
- creating an additional
package.json
file within thesrc
directory and defining"type": "module"
Any insight or guidance is much appreciated.