I got following error in as ESM Project on AWS Lambda Node 18.x:
{
"errorType": "Error",
"errorMessage": "Cannot find module '/var/task/src/storefront/api/checkout/preview' imported from /var/task/src/storefront/api/checkout/index.js",
"code": "ERR_MODULE_NOT_FOUND",
...
}
The error refers to this simple barelling in index.js:
export { preview } from './preview';
...
Adding .js to the export fixes the error:
export { preview } from './preview.js';
So not a path reference or permission problem. But that knowledge doesn't help me much because my code is automatically compiled from typescript to esm javascript and I would think that import without .js is standard. Anyone an idea why lambda makes this problems and how to fix it?
Here my package.json:
{
"name": "checkout",
"type": "module",
"main": "index.js",
"version": "0.1.0",
"engines": {
"node": ">=18.0.0"
},
...
tsconfig.js:
{
"compilerOptions": {
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"target": "es2022",
"outDir": ".build",
"moduleResolution": "node",
"lib": [
"es2022"
],
"skipLibCheck": true,
"rootDir": "./",
"typeRoots": [
"./node_modules/@types",
"./@types"
]
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
I deployed with serverless.yml (runtime: nodejs18.x
) and serverless-typescript plugin.
Looks similar to this question:
Cannot use ES6 on AWS Lambda function; how to import ES6 module within Lambda
TLDR:
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.