NodeJS 14.x - Native AWS Lambda Import/Export Support
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The real question behind this topic is whether AWS Lambda can run Node.js handlers written with ES module syntax instead of the older CommonJS pattern. For the Node.js 14.x Lambda runtime, the answer was yes, but only when the package and handler files were configured so Lambda and Node treated them as ECMAScript modules.
CommonJS Versus ES Modules in Lambda
Older Lambda examples usually looked like this:
That is CommonJS. With native ES module support, you can write the handler using import and export:
The syntax is cleaner for many modern JavaScript projects, but Lambda only accepts it when Node knows the file is an ES module.
How to Enable ES Module Behavior
You have two main ways to tell Node that your Lambda code uses ES modules:
- use the
.mjsfile extension - keep
.jsand set"type": "module"inpackage.json
Here is a minimal package file:
With that configuration, a file such as index.js may use import and export directly. If you do not set "type": "module", then plain .js files are treated as CommonJS and import syntax will fail.
The Lambda handler setting still points to the file stem and exported symbol, such as index.handler.
Interoperating With CommonJS Dependencies
Native ESM support does not mean every dependency in your project suddenly behaves the same way. Many packages still use CommonJS semantics, and mixed-module projects can get confusing if you convert everything blindly.
In many cases, importing a CommonJS package from an ES module works:
When you need the older require function inside an ES module, Node provides createRequire:
That gives you a controlled bridge for legacy code while keeping the handler itself in ESM format.
Practical Deployment Advice
If you are migrating an existing Lambda, convert one function at a time and test the packaging path carefully. Native ES module support solves the syntax problem, but it does not remove the need to verify:
- the handler name
- the archive structure
- dependency resolution
- local test behavior versus deployed behavior
This matters because mixed .cjs, .mjs, and .js files can work perfectly in local tooling while failing in Lambda if the runtime resolves them differently than you expected.
Common Pitfalls
- Writing
importsyntax in a.jsfile without using"type": "module"inpackage.json. - Assuming CommonJS and ESM packages expose identical import shapes.
- Mixing module systems across helper files without deciding which file extensions and package settings control the project.
- Forgetting that the Lambda handler still refers to the exported symbol name, even for ES modules.
- Migrating a whole codebase at once instead of converting and testing one function at a time.
Summary
- Node.js 14.x Lambda supported native ES modules when the package was configured correctly.
- Use
.mjsfiles or set"type": "module"to enableimportandexport. - ES module Lambda handlers use named exports such as
export const handler = .... - Mixed ESM and CommonJS projects can work, but they require deliberate configuration.
- Native support removes the need for transpilation just to get module syntax, not the need for deployment testing.

