I’m currently using CDK to develop my serverless application in AWS. However, I encountered an issue where the lambda function fails to import TypeScript modules as JavaScript modules post TS file compilation.
As a result, I am encountering a “Module not found” error when attempting to invoke my lambda function.
Here are the steps I follow before deploying the stack:
- tsc -> to compile TS files
- cdk synth
- cdk deploy
TypeScript configuration:
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": [
"es2018"
],
"noEmit": false,
"declaration": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"node_modules",
"cdk.out"
]
}
Code snippet:
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { DynamoDBClient, GetItemCommand, GetItemCommandInput } from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
export async function handler(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
console.log('event', event);
let userLat = event.pathParameters?.lat;
let userLng = event.pathParameters?.lng;
let tableName = process.env.TABLE_NAME;
let results;
const dbClient: DynamoDBClient = new DynamoDBClient({ region: "ap-south-1" });
let params: GetItemCommandInput;
if (tableName) {
params = {
TableName: tableName,
Key: marshall({
"lat": userLat,
"lng": userLng
}),
};
}
const run = async function () {
try {
const resp = await dbClient.send(new GetItemCommand(params));
results = unmarshall(resp.Item || {});
} catch(err) {
results = err;
}
};
run();
return {
body: JSON.stringify(
results
),
statusCode: 200
}; }
Additional details:
- Node: v14.20.0
- NPM: 6.14.17
- CDK: 2.40.0 (build 56ba2ab)
- TypeScript Version: 4.8.2
Error Message:
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@aws-sdk/client-dynamodb'\nRequire stack:\n- /var/task/fetchpartner.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-dynamodb'", "Require stack:", "- /var/task/fetchpartner.js", "- /var/runtime/UserFunction.js", "- /var/runtime/Runtime.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:221:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)", " at Object. (/var/runtime/index.js:43:34)", " at Module._compile (internal/modules/cjs/loader.js:1085:14)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", " at Module.load (internal/modules/cjs/loader.js:950:32)", " at Function.Module._load (internal/modules/cjs/loader.js:790:12)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)", " at internal/main/run_main_module.js:17:47" ] }