我尝试在VS代码中调试使用无服务器框架开发的无服务器应用程序。我关注了this文章。
但是当我尝试调试代码时,我从VS代码中收到错误,如下所示。
无法启动程序&#; g:\ Projects \ Serverless1 \ node_modules.bin \ sls&#39 ;;设置' 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",
"{}"
]
}
]
请帮我解决这个问题。
答案 0 :(得分:18)
我试图关注the same article,并遇到了同样的错误。添加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
答案 1 :(得分:1)
To get debugging to work with TypeScript I needed to add outFiles
set to the folder where my compiled code goes.
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
]
I haven't tried to debug straight JS but I would assume it's something like this.
"outFiles": [
"${workspaceRoot}/**/*.js"
]
答案 2 :(得分:0)
没有一个解决方案对我有用,因此这里是我作为资源所做的修改。而且,多个同事仅通过将自动附加功能打开并使用invoke局部关键字就可以发起攻击。
下面是一个具有launch.json的代码段,该代码最终对我有用。 / w为清楚起见,在我的函数命名为Processor的位置进行了注释。
-function或-f您要在本地调用的服务中函数的名称。
-path或-p指向json文件的路径,该文件包含要作为事件传递给调用函数的输入数据。此路径是相对于服务的根目录的。
-stage或-s要在其中调用函数的服务阶段。
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
}
]
}
答案 3 :(得分:0)
执行其他指南所述的内容,并使用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插件来做到这一点?