无服务器框架无服务器离线启动获取请求时出现错误找不到模块“node:url”(Lambda AWS)

Jor*_*tiz 5 node.js aws-lambda serverless-framework serverless serverless-offline

我有以下问题...事实证明我无法通过 Nodejs 的无服务器框架中的无服务器离线插件在本地测试我的 aws lambda 函数。无法执行简单的 GET 请求。

我可以使用serverless deploy命令部署到 aws lambda,但为了团队获得更好的开发体验,我们需要使用 serverless-offline 进行本地部署,我遵循了本指南https://fauna.com/blog/develop-using-serverless -offline,但是当我在成功运行命令后执行简单的获取请求时卡住了serverless offlineserverless offline这是我成功 运行时得到的无服务器离线命令输出

但是当我通过邮递员对链接执行获取请求时,http://localhost:3000/出现以下错误

获取请求错误

它基本上说找不到节点:url 模块,但我做了 npm install url,但仍然抛出同样的错误。我将在下面插入项目树和文件:

无服务器 hello world 树结构

处理程序.js

//"use strict";

console.log("AJAJAJAJAJAJAJAJAJAAJAJ");

console.log("Printing module");
console.log(module.exports);

module.exports.hello = async (event) => {
  console.log("whaaaaat");
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};
Run Code Online (Sandbox Code Playgroud)

无服务器.yml

org: ortizjorge97
app: aws-node-http-api-project
service: aws-node-http-api-project
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x

plugins:
  - serverless-offline
  - serverless-bundle
  - serverless-dotenv-plugin

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

Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "aws-node-http-api-project",
  "version": "1.0.0",
  "description": "<!-- title: 'AWS Simple HTTP Endpoint example in NodeJS' description: 'This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.' layout: Doc framework: v3 platform: AWS language: nodeJS authorLink: 'https://github.com/serverless' authorName: 'Serverless, inc.' authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' -->",
  "main": "handler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "serverless-bundle": "^5.5.0",
    "serverless-dotenv-plugin": "^4.0.2",
    "url": "^0.11.0"
  },
  "devDependencies": {
    "serverless-offline": "^10.2.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

我是 aws 的新手,所以我不知道会发生什么。

我在用

  • 节点 14.17.2
  • MacOS 蒙特利 m1 处理器

小智 0

您需要将实际的无服务器软件包本身安装为开发依赖项。尝试这个:

npm i -D serverless

注意:记得查看 serverless-offline 的文档以了解找到支持的 serverless 软件包版本。