Node Debug serverless offline using vscode
Asked Answered
T

3

10

I am using VS Code for development of AWS Lambda functions, I started using the serverless framework and the serverless offline library but, I am unable to use VS Code's debug mode to locally debug the code.

I am referring many sites, Following is one of them: https://medium.com/@OneMuppet_/debugging-lambada-functions-locally-in-vscode-with-actual-break-points-deee6235f590

My project structure is as follows:

enter image description here

Package.json:

enter image description here

launch.json:

enter image description here

I get the following error when I start debug:

enter image description here

Can someone please guide, with the correct configuration?

Theodor answered 29/4, 2019 at 4:53 Comment(3)
what does your launch.json config look like?Bonze
@Bonze updated the question with launch.jsonTheodor
Thank you Aniruddha. Still not able to get it goingBonze
M
11

in the package.json add debug script:

"scripts": {
.......
    "debug": "node --inspect node_modules/serverless/bin/serverless offline -s dev",
.........
}

VS code lunch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "name": "Serverless",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run",
        "debug"
      ],
      "port": 9229
    }
  ]
}

Then start debugging from VS code

Microsurgery answered 24/1, 2020 at 9:31 Comment(1)
Why on vscode output panel I wont get the same logging msgs as I would on a normal terminal session?Ballad
T
4

The warning you are seeing is a deprecation warning; the legacy debugger (--debug) has been deprecated since Node 7.7.0. The correct way to attach a node debugger to serverless offline is by using --inspect:

node --inspect $(npm bin)/sls offline start
Tremulant answered 30/4, 2019 at 2:53 Comment(6)
with this I get the the Debugger listening, however how do I actually get my debugging profile configured?Bonze
Do I put this in the debug script in package.json?Theodor
@AniruddhaRaje yeah, you can replace the debug script in package.json.Tremulant
hmmm ok, so what does $(npm bin) mean? I tried to run that exact code but no luck.Mcconaghy
thanks, it was exactly the answer I needed!Bivens
try using $(which sls) offlineSumptuous
O
0

If you have a valid sample event in JSON format AND you are OK with debugging one function at a time, then here is a configuration that has worked great for me. It enables breakpoints and step-through debuggin exactly as you'd expect:

    {
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "name": "sls invoke local: myFunction",
      "runtimeExecutable": "sls",
      "runtimeArgs": [
        "invoke",
        "local",
        "-f",
        "loadOptions",
        "-p",
        "activities/myFunction/myFunction-event.json"
      ],
    },
Overarch answered 3/2, 2023 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.