Cannot Debug In VS Code By Attaching To Chrome
Answer :
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 runserve -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.
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.
I came across this question when looking for help using the "Attach to Chrome" configuration in VSCode. While the accepted answer did give me a few hints, I did have to do some more digging. Here are the steps that worked for me in case anyone else finds them useful:
Install the Debugger for Chrome extension in VSCode
Serve your files with a web server of your choice
Launch Chrome with remote debugging enabled
In this new Chrome window navigate to the url that your web server is hosting (http://localhost:8080 for example).
In VSCode, add a configuration to your
launch.json
file that looks like this:"configurations": [ { "type": "chrome", "request": "attach", "port": 9222, "name": "Attach Chrome", "url": "http://localhost:8080/", "webRoot": "${workspaceFolder}" } ]
Press the play button in VSCode with the 'Attach to Chrome' option selected from the drop down.
The key thing needed in the configuration file is the url
field. This needs to be the URL where your files are being hosted and this URL needs to be currently open in the Chrome window that you just launched with remote debugging enabled. If you enter everything else right except this field, VSCode will give you an error message that says which urls are available. Something like Cannot connect to runtime process, timeout after 10000 ms - (reason: Can't find a valid target that matches: localhost:8080/. Available pages: ["http://localhost:8080",...
For the sake of completeness, here's how you launch Chrome with remote debugging enabled (from the Debugger for Chrome README):
Windows:
- Right click the Chrome shortcut, and select properties
- In the "target" field, append
--remote-debugging-port=9222
- Or in a command prompt, execute
<path to chrome>/chrome.exe --remote-debugging-port=9222
MacOS:
- In a terminal, execute
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Linux:
- In a terminal, launch
google-chrome --remote-debugging-port=9222
Comments
Post a Comment