[英]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 要在其中调用函数的服务阶段。
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 } ] }
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.