I have default config in launch.json. The site runs on port 8080.
{ "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "Launch Chrome against localhost", "url": "http://localhost:8080", "webRoot": "${workspaceRoot}" }, { "type": "chrome", "request": "attach", "name": "Attach to Chrome", "port": 9222, "webRoot": "${workspaceRoot}" } ] }
However, when I click on the Debug button, I get this error:
Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9222
Question 1: Why does VS Code assign port 9222 when creating this json?
What is so special about this port that MS decided to put it in this launch.json?
Question 2: What do I need to change to make things work?
The Launch debug always launches a new window. I am asking specifically about Attach debug option, so that it will open in a new tab instead.
To debug any project in either Chrome or Microsoft Edge, all you need to do is to start a session by pressing F5 or activating the debug icon in the menu bar and selecting “Run and debug”. Alternatively, you can also use the Visual Studio Code command palette and run the “Debug: Open Link” command.
When using the configuration url
, vscode will search for a tab with the EXACT url and attach to it if found.
Use the configuration urlFilter
, which can have wildcards like *, to attach the debugger to any sub route in your url.
e.g. "urlFilter": "http://localhost:4200/*"
The complete exacts steps to take:
configure your launch.json file to looks something like this:
{ "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "attach", "name": "Attach to Chrome", "port": 9222, "urlFilter": "http://localhost:4200/*", "webRoot": "${workspaceFolder}" } ] }
Close all opened chrome instances (make sure that all of them are killed using task manager in windows).
Launch chrome with the following parameter: --remote-debugging-port=9222
Make sure that the port in this parameter is the same as the one configured in 'port' property of the attache to chrome configuration in the launch.json file (like the example above)
You can add this parameter in your chrome shortcut properties by:
Right click your shortcut file and select properties
Chain it to Target
property, e.g.
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
Navigate to your site. In this example, http://localhost:4200
Run 'Start debugging' in VS Code.
You need to install Debugger for Chrome extension for this to work. Open extensions in VS Code and search for Debugger for Chrome
You need to run a web server on the URL specified in the first configuration (default to http://localhost:8080). I use serve
npm package that I installed globally. From my app folder I run serve -p 8080
Select Launch Chrome against localhost option. It will launch the browser and you can set breakpoints in your code and the debugging should work.
Regarding the second configuration (Attach to Chrome). There's nothing special about the port. In order to attach to Chrome you need to run Chrome with remote debugging enabled on port specified in the config. For example chrome.exe --remote-debugging-port=9222
. I personally never use this options. Just follow the three steps above and you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With