[英]AWS Lambda Function is returning "Cannot find module 'index'" yet the handler in the config is set to index
As my title explains I am getting the following error:正如我的标题所解释的,我收到以下错误:
{
"errorMessage": "Cannot find module 'index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
I have tried both solutions provided in creating-a-lambda-function-in-aws-from-zip-file and simple-node.js-example-in-aws-lambda我已经尝试了creating-a-lambda-function-in-aws-from-zip-file和simple-node.js-example-in-aws-lambda中提供的两种解决方案
My config currently looks like:我的配置目前看起来像:
and my file structure is:我的文件结构是:
and my index.js handler function looks like:我的 index.js 处理程序 function 看起来像:
exports.handler = function(event, context) {
What else could be causing this issue aside from what was stated in those two answers above?除了上述两个答案中所述,还有什么可能导致此问题? I have tried both solutions and I have also allocated more memory to the function just incase thats why it couldn't run.
我已经尝试了这两种解决方案,并且我还为 function 分配了更多的 memory 以防万一它无法运行。
EDIT - For the sake of trying, I created an even simpler version of my original code and it looked like this:编辑——为了尝试,我创建了一个更简单的原始代码版本,它看起来像这样:
var Q = require('q');
var AWS = require('aws-sdk');
var validate = require('lambduh-validate');
var Lambda = new AWS.Lambda();
var S3 = new AWS.S3();
theHandler = function (event, context) {
console.log =('nothing');
}
exports.handler = theHandler();
And yet still does not work with the same error?但仍然无法解决相同的错误?
Try zipping and uploading the contents of the folder lambda-create-timelapse.尝试压缩并上传 lambda-create-timelapse 文件夹的内容。 Not the folder itself.
不是文件夹本身。
If this was unclear for anyone else, here are the steps:如果其他人对此不清楚,请按以下步骤操作:
Step 1 Navigate to the folder of your project, and open that folder so that you are inside the folder:步骤 1导航到项目的文件夹,然后打开该文件夹,以便您位于该文件夹内:
Step 2 Select all of the images you want to upload into to Lambda:步骤 2选择要上传到 Lambda 的所有图像:
Step 3 Right-click and compress the files you have selected:步骤 3右键单击并压缩您选择的文件:
This will give you a .zip file, which is the file you need to upload to Lambda:这将为您提供一个 .zip 文件,这是您需要上传到 Lambda 的文件:
There are a lot of ways to automate this, but this is the manual procedure.有很多方法可以自动执行此操作,但这是手动过程。
I ran into this problem a few times myself, and this indeed has to do with zipping the folder instead of just the contents like you're supposed to.我自己遇到过几次这个问题,这确实与压缩文件夹有关,而不仅仅是像您应该压缩的内容。
For those working from the terminal...对于那些在终端工作的人......
While INSIDE of the directory where the .js files are sitting, run the following:在 .js 文件所在目录的 INSIDE 中,运行以下命令:
zip -r ../zipname.zip *
The *
is instructing the client to zip all the contents within this folder, ../zipname.zip
is telling it to name the file zipname.zip
and place it right outside of this current directory. *
指示客户端压缩此文件夹中的所有内容, ../zipname.zip
告诉它命名文件zipname.zip
并将其放在当前目录之外。
I had the same problem sometime ago - I reformatted the code.前段时间我遇到了同样的问题 - 我重新格式化了代码。
function lambdafunc1(event, context) {
...
...
...
}
exports.handler = lambdafunc1
The problem occurs when the handler cannot be located in the zip at first level.当处理程序无法位于第一级的 zip 中时,就会出现问题。 So anytime you see such error make sure that the file is at the first level in the exploded folder.
因此,每当您看到此类错误时,请确保该文件位于已分解文件夹中的第一级。
To fix this zip the files and not the folder that has the files.要修复此压缩文件,而不是包含文件的文件夹。
Correct Lambda
function declaration can look like this:正确的
Lambda
函数声明可能如下所示:
var func = function(event, context) {
...
};
exports.handler = func;
You may have other syntax errors that prevent the index.js
file from being properly ran.您可能有其他语法错误,导致
index.js
文件无法正常运行。 Try running your code locally using another file and using the index.js
as your own module.尝试使用另一个文件在本地运行您的代码,并将
index.js
用作您自己的模块。
make sure in your handler following code added确保在您的处理程序中添加以下代码
exports.handler = (event, context, callback) => {
...
}
Another reason this can occur is if you don't do an npm install
in the folder before packaging and deploying.发生这种情况的另一个原因是,如果您在打包和部署之前没有在文件夹中执行
npm install
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.