VS Code debug port mismatch - node.js

Details
In Win10. VS Code, Help, About...
[Window Title]
Visual Studio Code
[Content]
Version 1.14.2
Commit cb82febafda0c8c199b9201ad274e25d9a76874e
Date 2017-07-19T23:34:09.706Z
Shell 1.6.6
Renderer 56.0.2924.87
Node 7.4.0
Generated launch.json, adjusted with my runtime... settings
{
// Use IntelliSense to learn about possible Node.js debug 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",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm.cmd",
"runtimeArgs": [
"run", "functional-test"
],
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
}
Npm script referenced above...
"functional-test": "node test/functional/run-foo-tests",
First attempt
I then launch a debug session (F5), which in the Debug Console then yields...
Debugging with inspector protocol because a runtime executable is set.
npm.cmd --inspect=32825 --debug-brk run functional-test
Note the additional flags that aren't specified anywhere in my package.json or launch.json.
In addition to that message above, I am shown a popup in the IDE referencing a connection timeout error...
Cannot connect to runtime process, timeout after 10000 ms - (reason:
Cannot connect to the target: connect ECONNREFUSED
Second attempt
If I change the npm script as follows (include --inspect flag)...
"functional-test": "node --inspect test/functional/run-foo-tests",
The Debug Console states...
Debugging with inspector protocol because a runtime executable is set.
npm.cmd --inspect=17976 --debug-brk run functional-test
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
Note different port settings. At least the debugger is starting and giving me a port to try and target instead.
Where is it getting those values? How can I make them match (without manually editing both launch.json and npm script to assign the same port for both)?
It never successfully loads and instead I am shown the popup message at the top (mentioned above.)
Third attempt
If I edit the npm script to add the port...
"functional-test": "node --inspect=9229 test/functional/run-foo-tests",
And edit launch.json's port setting to match...
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm.cmd",
"runtimeArgs": [
"run", "functional-test"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
I then am rewarded with the following message in the Debug Console...
Debugging with inspector protocol because a runtime executable is set.
npm.cmd run functional-test
Debugger listening on port 5858.
Warning: This is an experimental feature and could change at any time.
{Expected console output...}
It will then work correctly (AFAIK!) Note that now the Debug Console's second line (echoing/logging the command ran) now matches the launch.json runtimeArgs settings (where it did not in the first and second attempts.)
Aside from skull-smashing brute force of trial and error, how was one supposed to figure this out (and surely there must be a better way?)
Fourth attempt
Trying to set the port both for the npm script call (obviously not the same as the node ... call in the npm script) and the port config param and it fails as in the first example.
"runtimeArgs": [
"run", "functional-test", "--inspect=5858"
],
"port": 5858,
Fifth attempt
Of course, as I was writing this, I finally found something like an answer via google...
"scripts": {
"debug": "node --nolazy --debug-brk=5858 myProgram.js"
},
Adjusting my script to match yields (in Debug Console)...
Debugging with inspector protocol because a runtime executable is
set. npm.cmd run functional-test (node:12420)
DeprecationWarning: node --debug is deprecated. Please use node
--inspect instead. Debugger listening on 127.0.0.1:5858
I am also shown a different popup at the top...
Cannot connect to runtime process, timeout after 10000 ms - (reason:
Cannot connect to the target: Parse Error).
Sixth attempt
If I change my script to match the info in the Debug Console...
"functional-test": "node --nolazy --inspect test/functional/run-foo-tests",
Debug Console says...
Debugging with inspector protocol because a runtime executable is set.
npm.cmd run functional-test
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
And popup is...
Cannot connect to runtime process, timeout after 10000 ms - (reason:
Cannot connect to the target: connect ECONNREFUSED 127.0.0.1

Related

VSCode debugger refuses to attach to node process with no error or logs?

We're using node v18.3.0
I'm starting my nestjs app, inside a docker container, with this command nest start -e \"node --inspect-brk\". Which gives the following output:
node#bfcdb32e737f:/opt/thallo/exchange-be$ npm run test:debug:wait
> exchange-be#0.0.1 test:debug:wait
> nest start -e "node --inspect-brk"
Debugger listening on ws://127.0.0.1:9229/c4d73a54-6165-4502-bcea-1fa4390db95c
For help, see: https://nodejs.org/en/docs/inspector
So it looks like it's waiting for a debugger to attach - excellent!
I have forwarded port 9229 on the docker container, docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfcdb32e737f exchange-be_exchange-be-uiapi "tail -f /dev/null" 7 minutes ago Up 7 minutes 0.0.0.0:3001->3001/tcp, :::3001->3001/tcp, 0.0.0.0:9229->9229/tcp, :::9229->9229/tcp exchange-be-uiapi
And I can telnet to that port from the host machine telnet localhost 9229:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
This is my launch.json:
{
// 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": [
{
"name": "Attach Exchange BE",
"port": 9229,
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node",
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"restart": true
}
]
}
But when I launch the debugger I just get this little progress bar for a few seconds and then the debugger stops:
But there's no error log or anything so I have no idea how to fix this. Any help is greatly appreciated!
For debugging a NodeJS application running a docker 127.0.0.1 interface wont reach the docker network try using 0.0.0.0.
In the debug command at the package json change it to nest start -e \"node --inspect-brk=0.0.0.0:9229\"
The 0.0.0.0 will tell the debugger to look at all available local network interfaces.
In the vscode debug configuration "address": "0.0.0.0" and it is recommended to use "remoteRoot" and "localRoot" directives for syncing the position of the current vscode work directory files to the files in the docker file system.

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

Breakpoints in Visual Studio Code not hit when debugging mocha tests

I'm using Mocha (and Chai) for my unit tests for a NodeJS module and want to debug it in Visual Studio code. I have a TypeScript file in the test subfolder with some tests. VScode generates the .js and .map file in the out dir (via tsc watch mode task). My tsconfig.json file contains these settings:
{
"compilerOptions": {
"compileOnSave": true,
"module": "commonjs",
"target": "es6",
"outDir": "out",
"removeComments": true,
"noImplicitAny": true,
"sourceMap": true,
"inlineSources": true,
"isolatedModules": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true
},
"include": [
"src/**/*", "parser/**/*", "test/**/*"
],
"exclude": [
"node_modules",
".vscode-test"
]
}
and the out dir contains 3 subdirs for the 3 includes. All fine so far.
I can run my tests using this command:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
outside of vscode. Then I ran this code with the --debug-brk switch and attached vscode to it. This works, but no breakpoint is hit. The configuration in launch.json for that is:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": true,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
Ideally, I'd like to have a run config so that I don't need to run mocha manually. With these settings I can at least run the tests:
{
"name": "Mocha",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"preLaunchTask": "tsc",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [ "--no-timeouts", "--colors", "${workspaceRoot}/out/test/**/*.js" ],
"stopOnEntry": true,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
}
"sourceMaps": true
}
but still, no breakpoint is hit.
What is required to make at least one of the 2 scenarios work?
Update: meanwhile I found by accident that breakpoints start working when you add a debugger; command somewhere in the test code and set at least one fresh breakpoint after it stopped on debugger;. After that all following breakpoints in this single file work as expected. Looks almost like a bug to me.
Using "protocol": "inspector", in the launch options helped me to continue for a while, even though this had the annoying side effect that the test process never stopped after everything was executed. I had to kill the task after each run. So I though i'd give it another try to find the problem and I succeeded. The solution is simple: add the outfiles option to your launch options, otherwise vscode will look for maps in the TS source folder. By adding:
"outFiles": [
"${workspaceRoot}/out/**/*.js"
],
everything started to work nicely. It would so helpfull if vscode would print a warning that it cannot find the source maps because of this missing setting.
situtaion
using Jasmine unit test (not Mocha)
when I click the Debug button on a unit test
-> it doesnt stop on the breakpoint, just runs to the end.
solution (in short)
(this may not apply to everyone's case)
it could be the port 9229 is taken by other process on your computer
try debugging in terminal, instead of in Vscode, and see what happens (I tried it in powershell)
eg: node --inspect-brk /usr/local/lib/node_modules/jasmine/bin/jasmine.js ~/exercism/javascript/leap/leap.spec.js
-> then the error shows up: Starting inspector on 127.0.0.1:9229 failed: permission denied (in my case)
find the process that is using the port & close it (lots online discussion on how to do this)
run netstat -ano | findstr 9229
or, you can find it in the Task Manager > Resource Monitor > Network > check your ports
run net stop hns && net start hns to restart your Host Network Service
I found this especially useful when you cannot find the port is taken by which process -- maybe due to that is a dynamic port
after resetting the port -> click the debug button on unit test -> should stop at breakpoint
---
situation & some comments (minor)
some online posts said:_ add the config in launch.json. doesnt help -- the default launch has no problem
the default config uses port 9229
some online posts said:_ use Chrome config to debug. feels unnecessary & its using the browser not nodejs
output panel in vscode Test Explorer shows nothing to indicate the error
this port issue can due to some software that mess around with dynamic port (eg: sometime could be related to some software that change your system proxy / ip).
(this once messed up with my other softwares eg: ActivityWatch)
reference (minor)
[]
"port": 9229,
<>
https://github.com/hbenl/vscode-jasmine-test-adapter
[]
1. run node --inspect-brk node_modules/mocha/bin/mocha test.js:
<>
https://youtrack.jetbrains.com/issue/WEB-43747
[]
I managed to start the debugger by running node --inspect-brk with the jasmine.js file called by Jasmine's CLI:
Kurts-MacBook-Pro:bin kurtpeek$ node --inspect-brk /usr/local/lib/node_modules/jasmine/bin/jasmine.js ~/exercism/javascript/leap/leap.spec.js
<>
How to drop into a debugger in a Jasmine test?
[]
Secondly, check if the port is in the excludedportrange by command "netsh int ipv4 show excludedportrange protocol=tcp".
Then check the dynamicport range by command "netsh int ipv4 show dynamicport tcp".
Set the start of dynamicport by command "netsh int ipv4 set dynamicport tcp start=49152 num=16384"
其次,用命令"netsh int ipv4 show excludedportrange protocol=tcp"检查是否在被排除的端口范围内。
然后用命令"netsh int ipv4 show dynamicport tcp"检查动态端口范围。
用命令"netsh int ipv4 set dynamicport tcp start=49152 num=16384"设置起始动态端口。
<>
https://github.com/eggjs/egg/issues/2432
[]
net stop hns && net start hns
<>
An attempt was made to access a socket in a way forbidden by its access permissions. Why?
[]
The port 9229 is the default debug port of the --inspect and --inspect-brk options.
<>
https://code.visualstudio.com/docs/nodejs/nodejs-debugging

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.

Visual studio code, debug not working

Hello i have problem with debug my app of node and visual studio code. When i tried run debug in visual i saw text " Debugger listening on port 30108
" but when i open my browser on localhost:30108 there is only information somethig like this
Type: connect
V8-Version: 4.5.103.36
Protocol-Version: 1
Embedding-Host: node v4.4.7
Content-Length: 0
on localhost:3000 (default app port) there is only error
" This site is unreachable "
So how to do, to be able to debug app with running app in browser ?
If you do a node app.js or npm start (or whatever for your project) in a terminal/command window, does your project also start successfully? Which OS are you using? Any firewall issues going on?
If you can access the site in your browser outside of VS Code, check my "Third attempt" documented here. Essentially, you need to edit both your launch.json and your package.json to indicate the port you are going to use. My examples follow the npm run {script name} format. You should be able to tailor it to suit.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm.cmd",
"runtimeArgs": [
"run", "start"
],
"port": 5858,
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
}
package.json
"scripts": {
"start": "node --inspect=5858 src/app.js",
}
Essentially, you need to ensure you are launching your app the same way you would from the command line. Then, ensure you have the matching port info in both files (and their respective locations) mentioned above.
More info available here for alternate/additional configuration options for debugging.

Resources