简体   繁体   中英

Can't debug serverless app in Visual Studio Code

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?

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",
        ]
    },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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