My Goal
Take a node program (a command-line server-side program, not a web application) and deploy it as a lambda function in AWS.
Background: my node development environment/approach
- Node 14.X on my MacBook Pro: version 14.2.0, in which ECMAScript module support is on by default
- Developing my software as ES6 modules as follows:
- Putting type: module in my package.json file
- Using the .js extension rather than .mjs and .cjs extensions (because VS Code intellisense does not play nice with the multi-extension approach )
- As this is a back-end program, I am not doing any transpiration or bundling (currently no Babel or Webpack)
Creating the lambda function
- Using latest node env supported by AWS (12.X)
- From what I can tell in the AWS lambda environment:
- The entry point is hard-coded in this case (index.js)
- There’s no way for me to pass command line arguments to node
- If I try to run my code unchanged (with type: module), it fails as expected, because AWS node is trying to ‘require’ my ES6 module:
- “Must use import to load ES Module: /var/task/index.js\nrequire() of ES modules is not supported. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /var/task/package.json.”
- If I try to change extensions on files to be explicit, AWS gags because it hard-codes the entry point to be ‘index.js’
- If I naively remove the type:module from package.json, it fails as expected because it wants everything to be CommonJS:
- “SyntaxError: Cannot use import statement outside a module"
- The entry point is hard-coded in this case (index.js)
Where I need help
From a couple hours of reading/research I’ve done, it seems like these are my options??:
- Rewrite my code to use CommonJS (undesirable, I want to use the modern features/approach)
- Set up either babel or a bundler like WebPack to convert my ES6 modules into CommonJS, and dump the converted code into the lambda
- Use ‘esm’
My Questions
- Am I correct to be going ‘all in’ on modules in my own code?
- Which approach above: #1, #2, #3 — or something else — is the easiest path to success here and the ‘proper’ approach?
Thank you!