This example demonstrates the officially supported file names and corresponding package.json files that instruct Lambda + Node.js to run in CommonJS or ES Module mode.
After cloning this repo:
npm i
npm startThen hit one of the following paths to see a basic JSON response, including a string property (whatev) that demonstrates the module system is working:
Contains a Node.js 14.x Lambda handler using CommonJS (require(), exports); handler is a single file with a CommonJS-specific file name: index.cjs
Contains a Node.js 14.x Lambda handler using CommonJS (require(), exports); handler is a single file with a generic JS file name: index.js; this demonstrates Node.js's default behavior
Contains a Node.js 14.x Lambda handler using ES Modules (import, export); handler is a single file with a ESM-specific file name: index.mjs
Contains a Node.js 14.x Lambda handler using ES Modules (import, export); handler is a single file with a generic JS file name: index.js, and a corresponding package.json file containing "type": "module", which overrides the default Node.js behavior, and loads index.js as an ES Module
Observe Architect's Lambda treeshaking functionality by running either npx arc deploy --dry-run or npx arc hydrate --autoinstall, both of which prepare all four Lambdas for production deployment.