阅读背景:

AWS Lambda错误:“无法找到模块'/var/task/index'”

来源:互联网 

Node.js Alexa Task Issue

节点。js Alexa的任务问题

I'm currently coding a Node.js Alexa Task via AWS Lambda, and I have been trying to code a function that receives information from the OpenWeather API and parses it into a variable called weather. The relevant code is as follows:

我目前正在为一个节点编码。通过AWS Lambda来执行js Alexa任务,我一直在尝试编写一个函数,该函数接收来自OpenWeather API的信息,并将其解析为一个名为weather的变量。相关代码如下:

var request = require('request');
var weather = "";
function isBadWeather(location) {
      var endpoint = "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&APPID=205283d9c9211b776d3580d5de5d6338";
      var body = "";
      request(endpoint, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                  body = JSON.parse(body);
                  weather = body.weather[0].id;
            }
      });
}

function testWeather()
{
      setTimeout(function() {
      if (weather >= 200 && weather < 800)
            weather = true;
      else
            weather = false;
      console.log(weather);
      generateResponse(buildSpeechletResponse(weather, true), {});
      }, 500);
}

I ran this snippet countless times in Cloud9 and other IDEs, and it seems to be working flawlessly. However, when I zip it into a package and upload it to AWS Lambda, I get the following error:

我在Cloud9和其他ide中无数次地运行这个代码片段,它似乎运行得非常完美。但是,当我将它压缩到一个包并将它上传到AWS Lambda中时,我得到以下错误:

{
    "errorMessage": "Cannot find module '/var/task/index'",
    "errorType": "Error",
    "stackTrace": [
        "Function.Module._load (module.js:276:25)",
        "Module.require (module.js:353:17)",
        "require (internal/module.js:12:17)"
    ]
}

I scoured countless articles and installed module-js, request, and many other Node modules that should make this code run, but nothing seems to fix this issue. Here is my directory, just in case:

我搜索了无数文章并安装了module-js、request和许多其他应该运行此代码的节点模块,但似乎没有什么可以解决这个问题。这是我的目录,以防万一:

- planyr.zip
   - index.js
   - node_modules
   - package.json

Does anyone know what the issue could be? Thank you so much in advance.

有人知道这是什么问题吗?非常感谢你。

5 个解决方案

#1


149  

Fixed it! The problem was trying to zip it using my Mac's compression software, itself. You're way better off running the following code in terminal when you are in the directory containing your index.js, node_modules, etc. files.

固定!问题是我试图用Mac的压缩软件压缩它。在包含索引的目录中,最好在终端中运行以下代码。js、node_modules等文件。

zip -r ../yourfilename.zip *

#2


9  

Update to the accepted answer: When this error occurs, it means your zip file is not in the valid form which AWS requires.

更新已接受的答案:当出现此错误时,意味着您的zip文件不是AWS所需的有效格式。

If you double click on zip you will find your folder inside that your code file,but lambda wants that when you double click on zip it shoud show direct code files.

如果你双击zip,你会在你的代码文件中找到你的文件夹,但是lambda想在你双击zip时显示直接的代码文件。

To achieve this:

为实现这一目标:

open terminal  
cd your-lambda-folder 
zip -r index.zip *

Then, upload index.zip to AWS Lambda.

然后,上传指数。邮政AWSλ。

#3


6  

Check that file name and handler name are same:

检查文件名和处理程序名是否相同:

bundle.ls file" referrerpolicy="no-referrer">

That means that zip file has bundle.js file that exports handler function:

这意味着zip文件有bundle。导出处理程序函数的js文件:

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

出口。handler =(事件、上下文、回调)=>{/…}

#4


4  

This is probably a permissions issue with files inside your deployment zip. Try chmod 777 your files before packaging them in a zip file.

这可能是部署压缩文件的权限问题。在将文件打包成zip文件之前,请尝试chmod 777。

#5


0  

In my case I had to replace

在我的情况下,我必须更换。

exports.handler = function eventHandler (event, context) {

with

exports.handler = function (event, context, callback) {

分享到: