Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
Cannot use ES6 on AWS Lambda function; how to import ES6 module within Lambda

I have a library foo which is written with ES6, import/export and in Typescript.

I have an app bar which uses foo. bar is also written with exports and Typescript. I would like to get bar running on AWS Lambda.

From what I can tell, I cannot use import/export in Lambda (with runtime node 14.x) as

export const handler = async () => {...};

Will error, but

exports.handler = async () => {...};

Will not.

So within my tsconfig I have set:

    "target": "ES5", (or ES6)
    "module": "CommonJS",

under the compiler options.

With this, running on Lambda I get errors since it now tries to require the foo library and says it must use import.

How can I achieve my end goal?

As I see it there are 3 options, none of which I know much about since I am still new to the JS nuances.

  • Have my bar app transpile, or use webpack, or whatever the tool may be to replicate import in cjs.
  • Have my foo library also include a CJS distrobution (started doing this and got many errors from the hoops I had to get through to make the library work in es6).
  • I'm missing something in Lambda / Typescript which will make this easy.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

ES modules support was added in Node v14. However, we need to help Node a bit in determining which type he has to use to load a file.

If you're only using ES modules, it's quite easy, you can just add "type": "module" to your package.json file in order to tell Node to use ES modules. Another way is to use a different file extension. However, since AWS Lambda needs the handler to be configured in the form of filename.methodname, it will always use the .js extension, which makes it not possible to use a different file extension for your entry point.

I have tested the "type": "module" in AWS Lambda, and this seems to work. I have not tested how interoperability would work with dependencies that use CommonJS modules, but I assume this would work out of the box.

You can refer to this blog for more information on how Node handles this.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!