AWS Lambda 错误:“找不到模块‘/var/task/index’"; [英] AWS Lambda Error: "Cannot find module '/var/task/index'"

查看:30
本文介绍了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 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?

推荐答案

已修复!我的问题是我尝试在 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 等文件).

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:

For Windows:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆