【问题标题】:AWS Serverless deployment error NodeJSAWS无服务器部署错误NodeJS
【发布时间】:2019-01-02 22:00:08
【问题描述】:

我有一个相当基本的 NodeJS API,我使用 NodeJS 中的 AWS 无服务器框架离线构建。我已经让它在离线配置中运行良好,但是当我尝试部署时,我最初收到以下日志错误:

Unable to import module 'services/tv/get/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)

我在 GitHub 上发现了一些线程在谈论类似的问题 in this thread,但是在查看它之后,我无法在我的情况下进行任何修复。我检查了部署的 zip 文件,它似乎具有所需的依赖项。

Serverless.yml

service: show-stoppers-lambda

provider:
  name: aws
  runtime: nodejs8.10
  memorySize: 128
  timeout: 10
  stage: production
  region: ap-southeast-1

functions:

  getNowAiringTv:
    handler: services/tv/get/handler.getNowAiringTv
    events:
      - http:
          path: tv/now
          method: get
          cors: true

  getTvShowDetails:
    handler: services/tv/get/handler.getShowDetails
    events:
      - http:
          path: tv/{showId}
          method: get
          cors: true

  getTvShowSearch:
    handler: services/tv/get/handler.getShowSearch
    events:
      - http:
          path: tv/search/{query}
          method: get
          cors: true

  getDiscoverTvFilter:
    handler: services/tv/get/handler.getDiscoverTv
    events:
      - http:
          path: tv/filter
          method: get
          cors: true

  getSimilarTvShows:
    handler: services/tv/get/handler.getSimilarShows
    events:
      - http:
          path: tv/similar/{showId}
          method: get
          cors: true

  getTvSeasonDetail:
    handler: services/tv/get/handler.getTvSeasons
    events:
      - http:
          path: tv/{showId}/{season}
          method: get
          cors: true

  getTvSeasonEpisodeDetail:
    handler: services/tv/get/handler.getTvEpisodes
    events:
      - http:
          path: tv/{showId}/{season}/{episode}
          method: get
          cors: true


plugins:
  - serverless-offline

Package.json

{
  "name": "show-stoppers-lambda",
  "version": "1.0.0",
  "description": "",
  "main": "handler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "alex",
  "license": "ISC",
  "devDependencies": {
    "serverless-offline": "^3.25.6"
  },
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "dotenv": "^6.0.0",
    "jsonwebtoken": "^8.3.0",
    "moment": "^2.22.2",
    "mongoose": "^5.1.5",
    "request": "^2.87.0"
  }
}

完整的代码在我的github here 上,我包含了一个package.zip here,其中包含内容(.env 之前已清空)。我希望有人能够告诉我我哪里出了问题我在使用 serverless-webpack 之前已经看到了一些建议但是对于像这样的小东西我不确定它是否需要,我也不知道如何设置它要么在这里工作。

更新

我发现我收到很多 500 错误的原因是由于请求,我仍然不清楚为什么依赖项似乎包含在 zip 中,但它可能缺少它需要的一个那。稍后我会详细查看它,但目前我已经通过使用 HTTPS 模块的简单实现来解决这个问题,现在请求都正确触发了

【问题讨论】:

  • 您可以编辑问题以使其更清晰吗?我以为您在加载处理程序函数时遇到问题 - Unable to import module 'services/tv/get/handler': Error。可能对其他人有用。

标签: javascript node.js amazon-web-services lambda serverless-framework


【解决方案1】:

您是否包含正确的文件?您的handler.js 代码似乎只有一个函数hello,需要引用为handler.hello。如果这只是一个用于在此处提问的虚拟文件,请尝试将 yaml 中的函数引用为 handler.<function-name>

【讨论】:

  • 是的,我忘了删除主文件中的一个虚拟文件,但 yml 指向 services/tv/get/handler,这样应该可以正常工作吗?
  • 我也尝试过重组,只是将基本处理程序更改为 service/tv/get 中的处理程序,它仍然有同样的问题,但它继续在本地工作
  • 我的意思是,更新您的 yaml 规范以将处理程序称为 handler: handler.getNowAiringTv。当然,当我将 services/tv/get/ 添加到工作无服务器 yaml 规范时,虽然 sls deploy 没有显示错误,但特定功能的 lambda 控制台出错了 The file services/tv/get/handler.js could not be found. Make sure your handler upholds the format: file-name.method. 所以只需从 yaml 规范中删除它,它就会起作用。跨度>
  • 在这种情况下这不是问题,它确实是导致错误的依赖请求,当我切换到使用 https 模块时它一切正常。我仍然不确定 Request here 的问题是什么,但它真的很烦人吗?无论如何感谢您的帮助@ASR
猜你喜欢
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 2020-07-07
  • 2021-02-28
  • 2020-08-20
  • 1970-01-01
相关资源
最近更新 更多