本文介绍了AWS Lambda错误:“找不到模块'/var/task/index'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js Alexa任务问题

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

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:

var request = require('request');
var weather = "";
function isBadWeather(location) {
      var endpoint = "http://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);
}

我在Cloud9和其他IDE中无数次运行了此代码段,并且似乎运行得很好.但是,当我将其压缩到一个程序包中并将其上传到AWS Lambda时,会出现以下错误:

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:

{
    "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)"
    ]
}

我搜寻了无数文章,并安装了应该使此代码运行的module-js,request和许多其他Node模块,但似乎没有任何方法可以解决此问题.这是我的目录,以防万一:

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:

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

有人知道这个问题可能是什么吗?提前非常感谢您.

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

推荐答案

解决了!我的问题是我尝试使用Finder中Mac内置的压缩​​功能压缩文件.

Fixed it! My issue was that I tried to zip the file using my Mac's built-in compression function in Finder.

如果您像我一样是Mac用户,则在项目的根目录(包含index.jsnode_modules等文件的文件夹)中时,应在终端中运行以下脚本./p>

If you're a Mac user, like me, you should run the following script in terminal when you are in the root directory of your project (folder containing your index.js, node_modules, etc. files).

zip -r ../yourfilename.zip *

对于Windows:

Compress-Archive -LiteralPath node_modules, index.js -DestinationPath yourfilename.zip

这篇关于AWS Lambda错误:“找不到模块'/var/task/index'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:54