-1

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 a index.mjs
  • using the "node" specific CDK construct
  • creating an additional package.json file within the src directory and defining "type": "module"

Any insight or guidance is much appreciated.

8
  • Are you sure you saw the same error message when you renamed index.js to index.mjs (and redeployed the function)? I'd expect, at worst, an import error but not a syntax error. Commented May 4, 2024 at 18:36
  • Yes, I did receive the exact same error, which I also thought was odd. Commented May 4, 2024 at 19:58
  • A simple test with index.mjs and foo.mjs with the former importing a function from the latter via 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. Commented May 5, 2024 at 16:45
  • I appreciate the suggestion. I did as you asked, updated my .js files to .mjs files, updated the paths, redeployed, and unfortunately, am getting the same exact syntax error as before. Commented May 6, 2024 at 1:55
  • Can you take CDK etc. out of the equation. Just create a trivial Node.js v20 index.mjs and foo.mjs, with the latter exporting a function and the former importing that function, as shown above. Use the AWS console to do this, not your current deployment automation. Commented May 6, 2024 at 12:27

1 Answer 1

2

Ultimately my issue came from following an AWS tutorial in which the lambda function was contained entirely within a single index.js file, and in my case my handler required modules outside of that file. I needed to change my cdk implementation to:

    // BEFORE
    const lambdaFn = new Lambda.Function(this, 'lambda-id', {
      runtime: Lambda.Runtime.NODEJS_20_X,
      handler: 'index.handler',
      code: new Lambda.InlineCode(fs.readFileSync('./src/index.js', { encoding: 'utf-8' }))
    });

    // AFTER
    const lambdaFn = new Lambda.Function(this, 'lambda-id', {
      runtime: Lambda.Runtime.NODEJS_20_X,
      handler: 'index.handler',
      code: Lambda.Code.fromAsset('./src', { exclude: ['*.js'] })
    });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.