【问题标题】:Serverless-offline throws "Configuration error" or "Cannot read property 'options' of undefined"Serverless-offline 抛出“配置错误”或“无法读取未定义的属性‘选项’”
【发布时间】:2021-05-09 05:31:44
【问题描述】:

我正在尝试使用 NodeJS、AWS Lambda、API Gateway、RDS 和 PostgreSQL 部署无服务器 REST API。

到目前为止,我已经成功设置了 PostgreSQL RDS,在开始编写函数来处理对数据库的请求之前,我认为最好先在本地测试一个小函数以检查请求是否正在执行正确处理。

所以在项目的根目录下,我安装了serverless-offline:

npm install serverless-offline

它在安装类型时抛出了几个警告:

npm WARN 已弃用 @hapi/pez@4.1.2:此版本已弃用,不再受支持或维护

(如果这些信息无关紧要,我很抱歉,我是新手,不知道什么重要什么不重要。)

然后我配置了我的 serverless.yml

service: serverless-node-postgres-rds-rest-api

app: serverless-app

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

plugins:
  - serverless-offline

configValidationMode: error

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: hello
          method: get

这是 handler.js

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

运行时出现问题

无服务器离线

当它抛出错误时:

Serverless: Running "serverless" installed locally (in service node_modules)
 
 Serverless Error ----------------------------------------
 
  Configuration error at 'functions.hello.events[0].httpApi.path': value 'hello' does not satisfy pattern /^(?:\*|\/\S*)$/
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.15.4
     Framework Version:         2.40.0 (local)
     Plugin Version:            4.5.3
     SDK Version:               4.2.2
     Components Version:        3.9.2

于是我把serverless.yml中的路径改成了“path: /hello”,错误改成了:

Type Error ----------------------------------------------
 
  TypeError: Cannot read property 'options' of undefined
      at module.exports (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/utils/telemetry/generatePayload.js:133:66)
      at async PluginManager.run (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/classes/PluginManager.js:607:35)
      at async Serverless.run (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/Serverless.js:325:5)
      at async /usr/local/lib/node_modules/serverless/scripts/serverless.js:634:9
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

如果我将路径更改为“路径:'*'”,它会抛出同样的最后一个错误。

我按照建议设置“SLS_DEBUG=*”环境变量后再次运行它,但结果几乎相同,没有额外的调试信息。

谁能告诉我我做错了什么?我花了几个小时上网寻找解决方法,但我没有找到解决同样错误的任何帖子,并且在 forum.serverless.com 上解决的问题提供了难以理解的纠缠信息。

谁能帮帮我?

【问题讨论】:

  • 自 2022 年 1 月 29 日起,serverless-websockets-plugin 已弃用。请参阅此链接以获取更新的文档:https://www.serverless.com/framework/docs/providers/aws/events/websocket/

标签: node.js aws-serverless serverless-offline


【解决方案1】:

如果您升级到无服务器版本 2.41.2 或更高版本,此问题将得到解决。

npm i -g serverless@2.41.2

【讨论】:

    【解决方案2】:

    我在尝试使用其他无服务器插件 (serverless-domain-manager) 时遇到了 Cannot read property 'options' of undefined 错误。将 Serverless 的版本降级到 2.38 或更早版本似乎可以解决此问题。

    【讨论】:

    【解决方案3】:

    遇到了同样的问题,但在从以前的项目切换到 package-lock.json 文件(相同的 package.json)后,问题得到了解决。所以我假设有一个依赖导致了这个问题,但是很抱歉我无法确定这个依赖是什么

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2018-12-12
      • 2018-08-16
      • 2019-06-10
      • 1970-01-01
      相关资源
      最近更新 更多