【问题标题】:How to Debug Amplify JavaScript functions in VS Code如何在 VS Code 中调试 Amplify JavaScript 函数
【发布时间】:2021-06-06 19:05:36
【问题描述】:

如何在 Windows 10 的 VS Code 上调试 Amplify JavaScript 函数?

这个问题出现在 github 上的 How to debug amplify function using visual studio code during invocation? 下,但它已经关闭并且很老了。例如,amplify invoke function 已被弃用,取而代之的是 amplify mock function

我试过这个launch.config:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Create Reort",
      "type": "node",
      "request": "launch",
      "program": "${env:APPDATA}/npm/node_modules/@aws-amplify/cli/bin/amplify",
      "args": [
        "mock",
        "function",
        "sayhello",
        "--event",
        "src/event.json",
        "--timeout",
        "0"
      ],
      "console": "integratedTerminal"
    }
  ]
}

这将记录输出,但不会在正在执行的函数内遇到任何断点:

设置步骤:

  1. 安装放大 cli

    npm install -g @aws-amplify/cli
    
  2. 初始化放大。选择 JavaScript 与任何框架。

    amplify init
    # Choose your default editor:                   Visual Studio Code
    # Choose the type of app that you're building:  javascript  
    # What javascript framework are you using:      none
    
  3. 添加功能

    amplify add function SayHello
    # Choose the runtime that you want to use:            NodeJS
    # Choose the function template that you want to use:  Hello World
    

【问题讨论】:

    标签: visual-studio-code windows-10 aws-amplify vscode-debugger aws-amplify-cli


    【解决方案1】:

    这是一个有点老套的解决方法。

    据我所知,对于单次执行(运行时环境中的特殊调味料、事件处理和自动可伸缩性),lambda 并没有什么魔力。 Amplify CLI 从event.json 传入event 对象并调用定义为处理程序的函数。你也可以在 vanilla node js 中做到这一点。

    创建一个类似debug.js 的文件——你可以把它放在任何你想要的地方,但我的在.vscode 目录中

    const { handler } = require("../amplify/backend/function/sayHello/src")
    const event = require("../amplify/backend/function/sayHello/src/event.json")
    
    // invoke
    const response = handler(event)
    
    console.log(response )
    

    然后你可以像这样使用普通的node js调试启动配置:

    {
      "name": "Debug Function",
      "program": "${workspaceFolder}/.vscode/debug.js",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "type": "pwa-node"
    }
    

    一些更友好/开箱即用的东西会很好,但这至少允许在没有太多额外工作的情况下逐步调试。

    【讨论】:

      【解决方案2】:

      这曾经在旧版本的 VS Code 中工作。我打开了https://github.com/aws-amplify/amplify-cli/issues/6894,希望能解决这个问题。

      同时,这是另一个建议的解决方法here,它使用了 Node 的注入器。将此代码添加到处理程序方法的顶部(确保在提交之前将其删除):

      require('inspector').open(9229, '127.0.0.1', true);
      debugger;
      

      请务必在 launch.json 中的放大模拟命令中设置足够长的超时时间。要单步执行代码,您可以通过导航到 about:inspect 并单击“Open dedicated DevTools for Node”来使用 Chrome 的 Node 调试器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-30
        • 2018-02-05
        • 2018-11-05
        • 1970-01-01
        • 2021-05-12
        • 2019-10-10
        • 2017-10-02
        • 2021-11-03
        相关资源
        最近更新 更多