Javascript AWS Lambda 函数返回“找不到模块‘索引’”,但配置中的处理程序设置为索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31664313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:59:53  来源:igfitidea点击:

AWS Lambda Function is returning "Cannot find module 'index'" yet the handler in the config is set to index

javascriptnode.jsamazon-web-servicesaws-lambda

提问by Andrew Font

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-fileand simple-node-js-example-in-aws-lambda

我已经尝试了创建-a-lambda-function-in-aws-from-zip-filesimple-node-js-example-in-aws-lambda 中提供的两种解决方案

My config currently looks like:enter image description here

我的配置目前看起来像:在此处输入图片说明

and my file structure is: enter image description here

我的文件结构是: 在此处输入图片说明

and my index.js handler function looks like :

我的 index.js 处理函数看起来像:

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.

除了上述两个答案中所述的内容之外,还有什么可能导致此问题?我已经尝试了这两种解决方案,并且我还为该函数分配了更多内存,以防万一这就是它无法运行的原因。

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?

但仍然无法解决相同的错误?

回答by tahsintahsin

Try zipping and uploading the contents of the folder lambda-create-timelapse. Not the folder itself.

尝试压缩并上传 lambda-create-timelapse 文件夹的内容。不是文件夹本身。

回答by Tyler

If this was unclear for anyone else, here are the steps:

如果其他人对此不清楚,请按以下步骤操作:

Step 1Navigate to the folder of your project, and open that folder so that you are inside the folder: Folder

步骤 1导航到项目的文件夹,然后打开该文件夹,以便您位于该文件夹内: 文件夹

Step 2Select all of the images you want to upload into to Lambda: Folder with selected files

步骤 2选择要上传到 Lambda 的所有图像: 包含选定文件的文件夹

Step 3Right-click and compress the files you have selected: Right-click menu with compress option highlighted

步骤 3右键单击并压缩您选择的文件: 突出显示压缩选项的右键单击菜单



This will give you a .zip file, which is the file you need to upload to Lambda:

这将为您提供一个 .zip 文件,这是您需要上传到 Lambda 的文件:

Archive.zip output

Archive.zip 输出



There are a lot of ways to automate this, but this is the manual procedure.

有很多方法可以自动执行此操作,但这是手动过程。

回答by Michael Du

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 *

zip -r ../zipname.zip *

The *is instructing the client to zip all the contents within this folder, ../zipname.zipis telling it to name the file zipname.zipand place it right outside of this current directory.

*是有指示客户端该文件夹内压缩所有内容,../zipname.zip告诉它来命名该文件zipname.zip并把它这个当前目录的外面。

回答by Naveen Vijay

I had the same problem sometime ago - I reformatted the code.

前段时间我遇到了同样的问题 - 我重新格式化了代码。

function lambdafunc1(event, context) {
...
...
...
}

exports.handler = lambdafunc1

回答by Sushil Ojha

The problem occurs when the handler cannot be located in the zip at first level. So anytime you see such error make sure that the file is at the first level in the exploded folder.

当处理程序无法位于第一级的 zip 中时,就会出现问题。因此,每当您看到此类错误时,请确保该文件位于已分解文件夹中的第一级。

To fix this zip the files and not the folder that has the files.

要修复此压缩文件,而不是包含文件的文件夹。

回答by vaquar khan

make sure in your handler following code added

确保在您的处理程序中添加以下代码

exports.handler =  (event, context, callback) => {
...
}

回答by kixorz

Correct Lambdafunction declaration can look like this:

正确的Lambda函数声明可能如下所示:

var func = function(event, context) {
   ...
};

exports.handler = func;

You may have other syntax errors that prevent the index.jsfile from being properly ran. Try running your code locally using another file and using the index.jsas your own module.

您可能有其他语法错误,导致index.js文件无法正常运行。尝试使用另一个文件在本地运行您的代码并将index.js用作您自己的模块。