简体   繁体   中英

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

Here is my launch.json and I run nodejs with express and it work fine.

{
"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceFolder}/app.js",
        "args": ["--env","local"]
    }
]

}

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.

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