Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Debug JavaScript in Visual Studio Code with live-server Running

Trying to debug a simple HTML and JavaScript project in Visual Studio Code. In the VS Code terminal window, 'live-server' is used to launch the 'index.html'. In VS Code, the 'Debugger for Chrome' is installed and configured. Even after the page is launched in the Chrome browser, the debugger will not attach and give the error message 'Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9222).'.

launch.json

"version": "0.2.0",
"configurations": [
    {
        "type": "chrome",
        "request": "attach",
        "name": "Attach to Chrome",
        "port": 9222,
        "webRoot": "${workspaceRoot}",
        "urlFilter": "http://localhost:8080"
    },
    {
        "type": "chrome",
        "request": "launch",
        "name": "Launch Chrome against index.html",
        "file": "${workspaceRoot}/index.html"
    }
]

The shortcut to Chrome is modified to include --remote-debugging-port=9222

Is it possible to use 'live-server' and debug or set breakpoints in VS Code on the JavaScript file?

like image 500
Stringfellow Avatar asked Oct 26 '17 04:10

Stringfellow


People also ask

Does Live server work with JavaScript?

This is a little development server with live reload capability. Use it for hacking your HTML/JavaScript/CSS files, but not for deploying the final site.

How do I open a JavaScript file on a live server?

Open a HTML file and right-click on the editor and click on Open with Live Server . Open the Command Pallete by pressing F1 or ctrl+shift+P and type Live Server: Open With Live Server to start a server or type Live Server: Stop Live Server to stop a server.


1 Answers

Works for me:

  1. Install Debugger for chrome extension on your VS Code.
  2. Install and activate Live Server extension on your VS Code.
  3. Press F5 and select Chrome:

enter image description here

  1. Check your Live Server port* and change the generated launch.json

enter image description here

  1. Set your break points, run Live Server and press F5:

enter image description here

  1. Enjoy :)

  • To check your Live Server port:
  1. Install Live Server & reload VS Code.
  2. Go to Preferences > Extensions > Live Server Config
  3. Scroll down to find Settings: Port and click "Edit in settings.json". Then you'll see the port number.
like image 163
Vinicius Gonçalves Avatar answered Oct 18 '22 00:10

Vinicius Gonçalves