Whenever I'm invoking my lambda function the following error occurs. I have tested various workarounds, but none of them seemed to work.
lambda function:
const AWS = require('aws-sdk');
const { nanoid } = require('nanoid');
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
try {
// Extract input data from request payload
const { input_text, input_file_path } = JSON.parse(event.body);
// Generate a unique ID for the item
const id = nanoid(22);
// Define the DynamoDB item
const params = {
TableName: 'Filetable',
Item: {
id,
input_text,
input_file_path,
},
};
// Save the item to DynamoDB
await dynamodb.put(params).promise();
// Return a successful response
const response = {
statusCode: 200,
body: JSON.stringify('Data saved to DynamoDB'),
};
return response;
} catch (error) {
// Return an error response if something goes wrong
const response = {
statusCode: 500,
body: JSON.stringify('An error occurred'),
};
return response;
}
};
Error :
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'nanoid'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'nanoid'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:997:17)",
" at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)",
" at async start (file:///var/runtime/index.mjs:1195:23)",
" at async file:///var/runtime/index.mjs:1201:1"
]
}
I have tried the above-given workarounds, although it still gives me the same error.
Can you please make sure that you are not following the steps on Mac, as that can cause problem sometime.
Yes I have tried them on Windows, yet they still give me the "can not import nanoid" error.