Can't debug serverless app in Visual Studio Code

守給你的承諾、 提交于 2019-12-24 12:19:33

问题


I'm trying to find out how can I use Visual Studio Code debugger to debug serverless lambda function. For testing purposes I have very simple test lambda function:

module.exports.handler = async (event) => {
  debugger;
  console.log(111, event);
};

Then, in launch.json I created such a configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch via NPM",
            "runtimeVersion": "8.4.0",
            "runtimeExecutable": "node",
            "program": "${workspaceFolder}/node_modules/.bin/sls",
            "cwd": "${workspaceRoot}",
            "args": [
                "invoke",
                "local",
                "--function",
                "test",
                "--data",
                "{foo:'bar'}"
            ],
            "port": 9229
        }
    ]
 }

Now, I'm puting a breakpoint inside my lambda function and pressing F5 to start debugger. I'm expecting then to go in code, put some watches and walk my code step-by-step. But nothing happens. No errors. No console outputs, no code paused on breakpoints. Nothing. All I get is message in debug console: /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}

If I go then in terminal and run that line I got exactly what is expected

set@set-home ~/www/blahblah/ens $ /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}
Serverless: INVOKING INVOKE
111 '{foo:bar}'

What am I doing wrong? How can I debug serverless application in Visual Studio Code?


回答1:


I suspect you just need to get rid of the "port": 9229 bit.

I've never specified a port for debugging Serverless functions locally, but adding it to any of my working configurations produces the symptoms you observe.


Incidentally, you might be able to take some of the other stuff out, as well. For reference, my Serverless debug configurations typically look like this for invoking locally:

    {
        "type": "node",
        "request": "launch",
        "name": "Debug sls local",
        "program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
        "args": [
            "invoke", "local", "--function", "processFirehose", "--path", "sample-event.json",
            "--stage",
            "nonprod",
        ]
    },


来源:https://stackoverflow.com/questions/51944673/cant-debug-serverless-app-in-visual-studio-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!