Skip to content
Advertisement

NodeJS 14.x – Native AWS Lambda Import/Export Support

I am looking to make use of the native import/export that comes with ES6.

I am using Serverless Containers within AWS Lambda.

I have my Dockerfile which looks like this:

JavaScript

I then have an app directory with my application code. The app.js code looks like this:

JavaScript

As you can see from this line import { success } from './utils/log'; I am making use of native imports.

In my package.json I specify this:

JavaScript

As I need to tell my application this is a module and I would like imports natively. If I don’t specify this, I get:

JavaScript

So, I specify it, telling Lambda this is a module. However, for the life of me I can’t get it to work, I am seeing this error:

JavaScript

It looks like /var/runtime/UserFunction.js is calling my app handler as a require and a module. However, I have no control over /var/runtime/UserFunction.js (I don’t believe?). In my Dockerfile I have specified Node14. I don’t quite know where I have gone wrong?

What I am looking to do is run the latest and greatest of Node14 (such as imports) without Babel/Transpiler that “bloat” my code. If someone could point me in the right direction of where I have gone wrong, it would be appreciated.

Advertisement

Answer

It appears that since yesterday there finally is native support for the ES6 module syntax in Node 14 lambdas – see https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement