繁体   English   中英

无法在 Visual Studio Code 中调试无服务器应用程序

[英]Cannot debug serverless application in Visual Studio Code

我试图调试在 VS 代码中使用无服务器框架开发的无服务器应用程序。 我关注了这篇文章。

但是当我尝试调试代码时,我从 VS 代码中收到一个错误,如下所示。

无法启动程序“g:\\Projects\\Serverless1\\node_modules.bin\\sls”; 设置 'outDir 或 outFiles' 属性可能会有所帮助。

sls 命令文件已存在于文件夹中,以下是 launch.json 文件设置

"version": "0.2.0",
"configurations": [

    {
        "type": "node",
        "request": "launch",
        "protocol": "inspector",
        "name": "run hello function",
        "program": "${workspaceRoot}\\node_modules\\.bin\\sls",
        "args": [
            "invoke",
            "local",
            "-f",
            "hello",
            "--data",
            "{}"
        ]

    }
]

请帮我解决这个问题。

我试图遵循同一篇文章,但遇到了同样的错误。 添加outFiles没有帮助,尽管它确实将我的错误消息更改为:

Cannot launch program 'd:\<path>\node_modules\.bin\sls' because corresponding JavaScript cannot be found.

我无法解释为什么 VSCode 在node_modules/.bin的可执行文件有问题,但如果我指向node_modules/serverless/bin ,事情会按预期工作:

"program": "${workspaceFolder}\\node_modules\\serverless\\bin\\serverless",

这是我的完整工作配置,其中我的测试事件 JSON 存在于项目根目录的sample-event.json中:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Lambda",
            "program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
            "args": [
                "invoke",
                "local",
                "-f",
                "<function-name>",
                "--data",
                "{}" // You can use this argument to pass data to the function to help with the debug
            ]
        }
    ]
}

使用无服务器 ^1.26.1、节点 8.9.4 LTS、VSCode 1.20.1

为了使用 TypeScript 进行调试,我需要将outFiles集添加到我编译的代码所在的文件夹中。

"outFiles": [
    "${workspaceRoot}/dist/**/*.js"
]

我没有尝试直接调试 JS,但我认为它是这样的。

"outFiles": [
    "${workspaceRoot}/**/*.js"
]

没有一个解决方案对我有用,所以这里是我作为资源的修改。 此外,多个同事只需将自动附加功能打开并使用 invoke 本地关键字即可进行攻击。

下面是一个包含 launch.json 的片段,它最终对我有用。 /w 注释为了清楚起见,我的函数被命名为处理器。

--function 或 -f 服务中要在本地调用的函数的名称。

--path 或 -p 保存要作为事件传递给调用函数的输入数据的 json 文件的路径。 此路径相对于服务的根目录。

--stage 或 -s 要在其中调用函数的服务阶段。

  • “无服务器”:“^1.30.3”
  • "serverless-plugin-typescript": "^1.1.5",
  • 节点:8.10.0
  • npm:5.6.0

     { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Debug Lambda", "program": "${workspaceFolder}/node_modules/.bin/sls", "args": [ "invoke", "local", "-f", "Processor", "-p", "./events/S3toLambda.json", "-s", "local" ], "autoAttachChildProcesses": true } ] }

2020 年更新:

执行其他指南所述的操作,并使用launch.json文件设置您的项目。

我遇到的问题是假设的文件"program": "${workspaceRoot}/node_modules/.bin/sls"抛出了一个错误。

我将其更改为"${workspaceRoot}/node_modules/serverless/bin/serverless"并且它起作用了。 这是完整的文件:

.vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Serverless debug",
            "program": "${workspaceRoot}/node_modules/serverless/bin/serverless",
            "args": [
                "invoke",
                "local",
                "-f",
                "hello",
                "--data",
                "{}"
            ]
        }
    ]
}

请注意,参数hello是我要调试的函数的名称。 我认为预期的用例必须是您为要调用的任何函数更改该文件名。 也许有人可以创建一个 VSCode 插件来做到这一点?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM