VSCode - debugger not hitting break-point on postman request - node.js

I'm building an express.js application that I'm testing using Postman. I haven't needed debugging until now, but I've just tried to set it up and run into a bit of an issue.
I followed this tutorial, but it isn't working for me as the end result of the tutorial shows that it should.
I built myself a launch.json file with a configuration built to handle running using nodemon...
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www"
}
]
I then started the debugger and got the following output in my terminal...
C:\Users\User\AppData\Roaming\npm\nodemon.cmd --inspect=35939 --debug-brk app.js
[nodemon] 1.14.11
[nodemon] to restart at any time, enter rs
[nodemon] watching: .
[nodemon] starting node --inspect=35939 --debug-brk app.js
Debugger listening on
ws://127.0.0.1:35939/fca1b410-e096-4189-a5b2-bf266755e89e
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
(node:24832) [DEP0062] DeprecationWarning: node --inspect
--debug-brk is deprecated. Please use node --inspect-brk instead.
To test it out, I put a break-point on the very first line of my app.js file. The break-point was hit successfully.
I went into the router file and put a break-point inside the post method that I needed to debug...
//POST New Trip page//
router.post('/newtrip', function (req, res) {
return tripCtrl.create(req, res); <---- BP on this line
});
I then ran the Postman request that I have been using to test this method:
(POST) http://localhost:3000/newtrip
I was fully expecting my new break-point to be hit, but the break-point wasn't hit, and I got no output from my debugger console or terminal. All I have is Postman returning 'Could not get any response'.
I'm well and truly lost as to where to go. Can anyone spot what might be wrong here please?
This is my file structure for the project:
Thanks,
Mark

This is the minimum configuration of lanuch.json required to debug in vscode based on your start command. Try updating with this to debug.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/bin/www"
}
]
}

Related

VS Code debugger is inconsistent while hitting breakpoints

I want to set up my VS Code debugger to debug my Next.js app. My launch.json contains the below settings:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"port": 9229,
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
]
}
]
}
I also added the debug script to my packege.json:
"debug": "NODE_OPTIONS='--inspect' next dev"
First, I run npm run debug in my terminal, then I launch the debugger in the VS Code Run and Debug tab. The debugger is attached, but when I start adding breakpoints, some of them work properly (the dot indicating the breakpoint is red and the app stops when the breakpoint is hit), but most of them are ignored despite the red indicator. There're also some unbound breakpoints.
I spent a few days figuring this out with no effect. I really look forward to moving past console-logging for debugging purposes.
VS Code version: 1.57.0

VS Code Debugger does not kill node process after stopping the debugger

I am working on a node.js application using express.js as a web framework listening on PORT 3000.
I am using VS Code v1.46.
My launch.json file is
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\WebApi\\index.js",
"restart": true,
"protocol": "inspector"
}
]
}
I am able to start the debugging session for the first time, but 2nd time onwards, I get error Error: listen EADDRINUSE: address already in use :::3000
This error is because VSCode didn't terminate the node.exe process created in 1st debugging session and so in the subsequent session node failed to start the express server on port 3000 as it is still in used.
Can anyone help me to configure VSCode to terminate node.exe process once I stop the debugger?
This issue started just last week for me, not sure why, maybe windows update.
Any way I found the following :
https://github.com/OmniSharp/omnisharp-vscode/issues/2387
where they say that if you add to launch.json
"console": "externalTerminal" or "console": 'integratedTerminal'
it will open a console for the process so that u can kill manually
It works fine with the Using the "preview" debug extension.
This is the launch.json for using that mode, just make sure that you put the correct command of your package.json.
{
"version": "0.2.0",
"configurations": [
{
"type": "node-terminal",
"name": "Run Script: start",
"request": "launch",
"command": "npm run start",
"cwd": "${workspaceFolder}"
}
]
}

VSCode Nodejs debugger does not save changes?

I am just starting to learn using the Nodejs debugger. It has been really helpful already but I spent a a lot of time changing a js file I was debugging but I did not know that the changes I made were not being implement right away.
Is this an expected behavior? Can I set the debugger up so that it restarts on each save and notices the new changes?
Edit 1 :
Here is my debuf config:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app.js"
}
]
}
Normally, after every save, you have to restart your node program to see the changes. I'm sure there are other programs but one I use exclusively (I never use node alone) is nodemon. It's globally installed npm package program. Here is sample config for vs code debugger:
{
"type": "node",
"request": "launch",
"name": "Launch app - nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--inspect=9250"
],
"program": "${workspaceRoot}/api/app",
"cwd": "${workspaceRoot}/api",
"autoAttachChildProcesses": true,
"restart": true
},
nodemon will monitor all the files and restart node every time it detects changes.
Add runtimeArgs to change debugger port.

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

Can I run/debug Heroku Node.js app locally with VSCode?

TL; DR
On Microsoft VSCode v1.6.1, how to:
Properly set runtime executable?
Properly pass Heroku arguments?
Run Heroku Node.js app?
Debug Heroku Node.js app?
Details
I have created a Heroku Node.js application, which is launched using the CLI command:
heroku local web
and successfully starts at port 5000.
I am trying to debug it using Microsoft Visual Studio Code, using the following launch.json configuration:
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/app.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": "/usr/local/bin/heroku",
"runtimeArgs": [
"local web",
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outFiles": []
}
But VSCode is automagically passing --debug-brk argument to heroku, causing the error:
/usr/local/bin/heroku --debug-brk=23080 'local web' app.js
! `--debug-brk=23080` is not a heroku command.
! See `heroku help` for a list of available commands.
VSCode also does not find heroku command without its full path (seems like it is not loading PATH environment variable).
Any ideas about how to setup the editor?
The following solution works for me:
1) In your procfile add the parameter --debug to the node process
web: node --debug server.js
By default the debugger listens in the port 5858
2) Once you have the node process running, open VSCode and add the following configuration to your launch.json file
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
3) Finally, click the play button in VSCode with the option "Attach to Process" and it should debug your process.
The following solution worked for me.
In my package.json "scripts", I added:
"debug": "node --inspect-brk server.js"
Then, in launch.json I added an envFile entry to the default "Launch via NPM" configuration, which now looks looks like this:
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 9229,
"envFile": "${workspaceFolder}/.env"
}
The above solution enables the VSCode debugger to run my server via an npm script, and my server runs with the env vars set in my .gitignore'd .env file, just like in the "regular" Heroku node.js workflow.
I struggled with this as for some reason the solution propsed didn't work for me. However, an alternate solution did so I thought I would share.
From the default debugging options in VS Code choose
Attach by Process ID
When you start the debugger with this configuration it should list available processes to attach to and one should be simply be server.js. This requires manually attaching each time, and if the other automatic attachment works for you that may be better, but this is still a workable solution.

Resources