I am trying to invoke a lambda function on AWS but I am getting the error: “Error: Cannot find module ‘index’nRequire stack:n- /var/runtime/index.mjs”
Folder structure:
enter image description here
index.ts Code:
// SPDX-License-Identifier: MIT-0
import { APIGatewayProxyResult } from 'aws-lambda';
import * as AWSCore from 'aws-sdk';
import * as AWSXRay from 'aws-xray-sdk-core';
let AWS;
const ddbOptions: AWSCore.DynamoDB.Types.ClientConfiguration = {
apiVersion: '2012-08-10'
};
// https://github.com/awslabs/aws-sam-cli/issues/217
if (process.env.AWS_SAM_LOCAL) {
AWS = AWSCore;
ddbOptions.endpoint = 'http://dynamodb:8000';
} else {
AWS = AWSXRay.captureAWS(AWSCore);
}
async function handler(): Promise<APIGatewayProxyResult> {
let response: APIGatewayProxyResult;
try {
const client = new AWS.DynamoDB(ddbOptions);
const params: AWS.DynamoDB.Types.ScanInput = {
TableName: process.env.TABLE || 'books'
};
const result: AWS.DynamoDB.Types.ScanOutput = await client.scan(params).promise();
const bookDtos = result.Items?.map(item => ({
isbn: item['isbn'].S,
title: item['title'].S,
year: parseInt(item['year'].N!, 10),
author: item['author'].S,
publisher: item['publisher'].S,
rating: parseInt(item['rating'].N!, 10),
pages: parseInt(item['pages'].N!, 10)
}));
response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(bookDtos)
};
} catch (e) {
response = {
statusCode: 500,
headers: {},
body: ''
};
}
return response;
}
export { handler };
package.json file:
{
"name": "books-get-all",
"version": "1.0.0",
"description": "Lambda function to get all books",
"main": "index.js",
"scripts": {
"prebuild": "mkdir -p dist",
"copy-deps": "cp -r node_modules dist",
"build-dev": "tsc",
"build": "npm run prebuild && npm i && tsc && npm prune --production && npm run copy-deps",
"test": "mocha -r ts-node/register ./tests/*.spec.ts"
},
"author": "Amazon Web Services",
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "^8.10.64",
"@types/chai": "^4.2.13",
"@types/mocha": "^8.0.3",
"@types/node": "^14.0.27",
"@types/sinon": "^9.0.8",
"@types/sinon-chai": "^3.2.5",
"@types/uuid": "^8.3.0",
"aws-sdk": "^2.1354.0",
"aws-sdk-mock": "^5.1.0",
"chai": "^4.2.0",
"mocha": "^9.1.3",
"sinon": "^9.2.0",
"sinon-chai": "^3.5.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7",
"uuid": "^8.3.1"
},
"dependencies": {
"aws-xray-sdk-core": "^3.1.0"
}
}
Error:
{
“errorType”: “Runtime.ImportModuleError”,
“errorMessage”: “Error: Cannot find module ‘index’nRequire stack:n- /var/runtime/index.mjs”,
“trace”: [
“Runtime.ImportModuleError: Error: Cannot find module ‘index'”,
“Require stack:”,
“- /var/runtime/index.mjs”,
” at _loadUserApp (file:///var/runtime/index.mjs:1087:17)”,
” at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)”,
” at async start (file:///var/runtime/index.mjs:1282:23)”,
” at async file:///var/runtime/index.mjs:1288:1″
]
}
I am using Node.js 20.x
I tried changing index.ts to index.js as well as index.mjs but it is not working
Shreya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.