Node.js Alexa Task Issue

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

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时,会出现以下错误:

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

我安装了模块js、请求和许多其他 node 模块,这些模块应该可以让代码运行,但似乎没有任何东西可以解决这个问题.这是我的目录,以防万一:

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

有人知道问题是什么吗?

推荐答案

修好了!我的问题是,我试图使用Mac内置的Finder压缩功能压缩文件.

如果你是Mac用户,像我一样,当你在项目的根目录(包含index.jsnode_modules等文件的文件夹)中时,你应该在terminal中运行以下脚本.

zip -r ../yourfilename.zip *

对于Windows:

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

Node.js相关问答推荐

Spotify Auth访问令牌给出错误代码400

如何解决TypeError:requ.isAuthenticated不是函数错误?

Node.js PNG缓冲区获取未知图像格式错误

TS 后端开发:prismagenerate找不到已安装的@tsed/prisma包

将 POST 的 json 变量格式化为 lambda

node_modules/preact/src/jsx.d.ts:2145:22 - 错误 TS2304:找不到名称SVGSetElement

使用 NPM 三个 mocha+typescript 进行测试

如何使用 NodeJS 加密模块将 ECDH 密钥转换为 PEM 格式

为什么 FastAPI 需要 Web 服务器(即 Nginx)而不是 Express API?

如何在没有云功能的情况下使用 Google PubSub

错误:找不到模块'C:\Users\nguye\AppData\Local\nodejs\node_modules\npm\bin\npm-cli.js'

后端位于 Docker 容器中时的 SvelteKit SSR fetch()

Winston http 日志(log)级别的行为与 info 不同

根据 mongoDB 中存在的唯一字符串生成唯一字符串的最佳方法

如何为一个网站实现这 2 个网址.即 www.administrator.sitename.com 和 www.sitename.com?

为什么 JavaScript 的 parseInt(0.0000005) 打印5?

使用 pg-promise 进行多行插入

npm package.json 操作系统特定脚本

node.js 模块和函数中this的含义

当进程被杀死时,如何优雅地关闭我的 Express 服务器?