gpt4 book ai didi

node.js - 如何在aws lambda函数中添加外部模块?

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:17 25 4
gpt4 key购买 nike

我创建了一个 AWS Lambda 函数,用于使用 API Gateway 将Item放入dynamoDB表中。

在 lambda 函数中,我添加了一个用于创建 userId 的模块UUID。我已经使用内联代码创建了 Lambda 函数。

现在我的问题是我收到错误,“找不到模块'uuid'”

因为它是一个外部模块。那么,谁能帮我解决这个问题。如何在 lambda 函数中添加此模块并使用它?

下面是我的 lambda 函数 -

'use strict';
const uuid = require('uuid');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB();

exports.handler = function(event, context) {

var tableName = "SiplAwsAPI_users";
var datetime = new Date().getTime().toString();

dynamodb.putItem({
"TableName": tableName,
"Item": {
"userId": {"S": uuid.v1()},
"timedate": {"S": datetime},
"userName": {"S": event.userName},
"userPassword": {"S": event.userPassword},
}
}, function(err, data) {
if (err) {
var response= {"response":"false", "message":JSON.stringify(err.message, null, ' '),"data":JSON.stringify(err.statusCode, null, ' ')};
context.succeed(response);
} else {
//console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
var response= {"response":"true", "message":"Register Successfully","data":JSON.stringify(data, null, ' ')};
context.succeed(response);
}
});

}

这是错误-

{
"errorMessage": "Cannot find module 'uuid'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)",
"Object.<anonymous> (/var/task/index.js:3:14)",
"Module._compile (module.js:409:26)",
"Object.Module._extensions..js (module.js:416:10)",
"Module.load (module.js:343:32)",
"Function.Module._load (module.js:300:12)",
"Module.require (module.js:353:17)"
]
}

最佳答案

要包含 NPM 依赖项,您需要使用上传功能。为此,您需要创建一个目录并将其包含所有依赖项进行压缩。

为了简化 devOps 的过程,您可以考虑使用 serverless framework

关于node.js - 如何在aws lambda函数中添加外部模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42969821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com