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.jsfile to aindex.mjs - using the "node" specific CDK construct
- creating an additional
package.jsonfile within thesrcdirectory and defining"type": "module"
Any insight or guidance is much appreciated.
import { somefunc } from "./foo.mjs"works fine, imports the function, and the exported function is callable. Also, make sure you re-deploy the Lambda function after making changes..jsfiles to.mjsfiles, updated the paths, redeployed, and unfortunately, am getting the same exact syntax error as before.