node.jsexpressdebuggingecmascript-6visual-studio-code

How to debug nodeJS (ExpressJS) app in visual studio code?


I have tried many different solutions available online but nothing works in my case, I am trying to debug a nodeJS app while its running, invoking the API through UI/postman

My launch.json: took guide from here

{
        "type": "node",
        "request": "attach",
        "name": "Attach by Process ID",
        "processId": "${command:PickProcess}",
        "skipFiles": [
            "<node_internals>/**"
        ]
}

After starting local server, when I start debugger, it ask to pick a process, I select the one ending with --exec babel-node server.js it attaches successfully but deosn't load my project scripts, only node_modules, eval and node_internal.

In my code if I put break point, I see this error "Break point set but not yet bound"

My package.json start sctipt:

"scripts": {
    "start": "nodemon --exec babel-node server.js"
}

My code is in ES6, I start the server though a shell script which first set some environment then do npm start

My .bablerc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

Solution

  • Turns out I was picking wrong process ID. When we start the debugger, it shows us the list of running processes to select with which debugger will be attached. I picked the one ending with _babel-node server.js instead of --exec babel-node server.js. Doing this loaded all my script and started working as expected.