简体   繁体   English

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

[英]Cannot debug serverless application in Visual Studio Code

I have tried to debug serverless application developed using serverless framework in VS code.我试图调试在 VS 代码中使用无服务器框架开发的无服务器应用程序。 I have followed this article.我关注了这篇文章。

But when I trying to debug the code I'm getting an error from VS code as below.但是当我尝试调试代码时,我从 VS 代码中收到一个错误,如下所示。

Cannot launch program 'g:\\Projects\\Serverless1\\node_modules.bin\\sls';无法启动程序“g:\\Projects\\Serverless1\\node_modules.bin\\sls”; setting the 'outDir or outFiles' attribute might help.设置 'outDir 或 outFiles' 属性可能会有所帮助。

sls command file already exists in the folder and following are the launch.json file settings 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",
            "{}"
        ]

    }
]

Please help me to fix this issue.请帮我解决这个问题。

I attempted to follow the same article , and experienced the same error.我试图遵循同一篇文章,但遇到了同样的错误。 Adding outFiles didn't help, although it did change my error message to:添加outFiles没有帮助,尽管它确实将我的错误消息更改为:

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

I can't explain why VSCode has a problem with the executable in node_modules/.bin , but if I point at node_modules/serverless/bin instead, things work as expected:我无法解释为什么 VSCode 在node_modules/.bin的可执行文件有问题,但如果我指向node_modules/serverless/bin ,事情会按预期工作:

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

Here is my full working configuration, where my test event JSON exists in sample-event.json in the project root:这是我的完整工作配置,其中我的测试事件 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
            ]
        }
    ]
}

Using Serverless ^1.26.1, Node 8.9.4 LTS, VSCode 1.20.1使用无服务器 ^1.26.1、节点 8.9.4 LTS、VSCode 1.20.1

To get debugging to work with TypeScript I needed to add outFiles set to the folder where my compiled code goes.为了使用 TypeScript 进行调试,我需要将outFiles集添加到我编译的代码所在的文件夹中。

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

I haven't tried to debug straight JS but I would assume it's something like this.我没有尝试直接调试 JS,但我认为它是这样的。

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

None of the solutions worked for me so here is my modification as a resource.没有一个解决方案对我有用,所以这里是我作为资源的修改。 Also, multiple coworkers were able to attack just by flipping auto-attach to on and using the invoke local keywords.此外,多个同事只需将自动附加功能打开并使用 invoke 本地关键字即可进行攻击。

Below is a snippet featuring the launch.json that eventually worked for me.下面是一个包含 launch.json 的片段,它最终对我有用。 /w comments for clarity where my function is named Processor. /w 注释为了清楚起见,我的函数被命名为处理器。

--function or -f The name of the function in your service that you want to invoke locally. --function 或 -f 服务中要在本地调用的函数的名称。

--path or -p The path to a json file holding input data to be passed to the invoked function as the event. --path 或 -p 保存要作为事件传递给调用函数的输入数据的 json 文件的路径。 This path is relative to the root directory of the service.此路径相对于服务的根目录。

--stage or -s The stage in your service you want to invoke your function in. --stage 或 -s 要在其中调用函数的服务阶段。

  • "serverless": "^1.30.3" “无服务器”:“^1.30.3”
  • "serverless-plugin-typescript": "^1.1.5", "serverless-plugin-typescript": "^1.1.5",
  • node: 8.10.0节点:8.10.0
  • npm: 5.6.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 } ] }

Update for 2020: 2020 年更新:

Do what the other guides state and setup your project with a launch.json file.执行其他指南所述的操作,并使用launch.json文件设置您的项目。

The problem I had was that the supposed file "program": "${workspaceRoot}/node_modules/.bin/sls" threw an error.我遇到的问题是假设的文件"program": "${workspaceRoot}/node_modules/.bin/sls"抛出了一个错误。

I changed it to "${workspaceRoot}/node_modules/serverless/bin/serverless" and it worked.我将其更改为"${workspaceRoot}/node_modules/serverless/bin/serverless"并且它起作用了。 Here's the full file:这是完整的文件:

.vscode/launch.json: .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",
                "{}"
            ]
        }
    ]
}

Be aware that the argument hello is the name of the function I want to debug.请注意,参数hello是我要调试的函数的名称。 I think the intended use case must be that you change that file name for whatever function you want to invoke.我认为预期的用例必须是您为要调用的任何函数更改该文件名。 Maybe someone could create a VSCode plugin to do this?也许有人可以创建一个 VSCode 插件来做到这一点?

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

相关问题 无法在Visual Studio代码中调试无服务器应用程序 - Can't debug serverless app in Visual Studio Code 如何使用另一个端口在 Visual Studio Code 中调试无服务器脱机? - How to debug Serverless Offline in Visual Studio Code using another port? 无法在Visual Studio代码中调试Node.js应用程序 - Unable to debug the Nodejs application in Visual studio code 调试Express js应用程序Visual Studio代码: - debug express js application visual studio code: 如何使用无服务器捆绑包在无服务器框架项目中调试 Visual Studio Code 中的测试? - How can I debug tests in Visual Studio Code with a serverless-framework project using the serverless-bundle package? Visual Studio Code Node.js调试失败-请求``启动&#39;&#39;:无法启动 - Visual studio code nodejs debug failure - request 'launch': cannot launch Visual Studio代码中Ionic2-Angular2应用程序的Typescript调试 - Typescript debug for Ionic2-Angular2 application in Visual Studio code 在 Visual Studio 代码中调试不起作用 - Debug in visual studio code is not working Visual Studio Code 无法调试 Electron - Visual Studio Code unable to debug Electron 无法在Visual Studio Code中调试TypeScript - Cant debug TypeScript in Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM