search
HomeDevelopment ToolsVSCodeA brief discussion on several methods of debugging js code in VSCode

How to debug js code in VSCode? The following article will introduce to you several methods of debugging js code in VSCode. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on several methods of debugging js code in VSCode

Related recommendations: "vscode tutorial"

Introducing two methods of debugging js in VS Code:

  • Quokka.js plug-in

  • Debugger for Chrome with Live Server

Quokka.js plug-in

Plug-in address: https://marketplace.visualstudio.com/items?itemName=WallabyJs.quokka-vscode

In the extension store Search and install:

Use the shortcut key ctrl shift P in VS code to bring up the panel and enter quo:

You can see that there are two options, one is to debug js and the other is to debug ts. We choose the first one.

There may be a prompt asking you whether to buy the pro version. You can turn it off and ignore it, or you can choose to buy it.

Try to enter a few lines of code in the open edit box:

You will find that the code runs in real time. The green square on the left represents that the statement was successfully executed. If the execution was unsuccessful, it will turn red.

Debugger for Chrome is paired with Live Server

Debugger for Chrome plug-in address: https:// marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome

Live Server plug-in address: https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer

Please download and install these two plug-ins first; this method requires the installation of Google Chrome.

Suppose you have such a front-end project:

After you install Live Server you will find VS Code There is a Go Live small button in the lower right corner:

Click it!

Live Srever will create a local development server and pop up a browser window with an address similar to this:

http://127.0.0.1:5500/

There will also be a prompt:

You can temporarily close this browser window, don’t worry, it won’t happen again. When you click the small button in the lower right corner or close VS Code, you can still open the web page just now through http://127.0.0.1:5500/.

Please remember this port number: 5500, because it will be used later. When you open Live Server or other programs that occupy this port number, the port number will automatically be 1.

As the name suggests, Live Server is updated in real time. When you modify the project files or code, Live Server will update immediately and automatically refresh the page.

Return to the editor.

Click in turn: Run icon-> Create launch.json file:

Overwrite the original configuration with the following configuration and save:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "chrome 调试",
            "url": "http://localhost:5500",
            "webRoot": "${workspaceFolder}"
        }
    ]
}

VS Code will save the relevant configuration in the directory where the project is located:

Note: "url" The port number 5500 in the field must correspond to the port number provided by Live Server.

Click:

You will see:

As you wish, it has It's working normally.

We move the mouse to the icon position and click to set a breakpoint:

Then click the button on the web page:

你会看到打上的断点成功拦截执行:


细心的你可能会发现,当点击一次 Go Live 按钮时会打开一个浏览器窗口,再点击一次调试按钮时(快捷键 F5),会打开第二个用于调试的窗口。这可能是没有必要的,我们只需要一个窗口就行了。

下面我们来编辑配置,使这个过程只需打开一次浏览器窗口。

点击 “chrome 调试” 旁边的齿轮:

A brief discussion on several methods of debugging js code in VSCode

在打开的编辑框依次点击:

A brief discussion on several methods of debugging js code in VSCode

得到如下配置并保存:

A brief discussion on several methods of debugging js code in VSCode

在扩展商店搜索 Live Server 并打开它的扩展设置:

A brief discussion on several methods of debugging js code in VSCode

需要修改的配置有两个:

A brief discussion on several methods of debugging js code in VSCode

1、Chrome Debugging Attachment

A brief discussion on several methods of debugging js code in VSCode

点击进入,将 "liveServer.settings.ChromeDebuggingAttachment": false 改为 true。

"liveServer.settings.ChromeDebuggingAttachment": true

2、Custom Browser

点击此选项中的下拉框,我们选择 chrome:

A brief discussion on several methods of debugging js code in VSCode

修改完成!

接下来我们重启 Live Srever 服务:

A brief discussion on several methods of debugging js code in VSCode

回到 VS Code,按下 F5 键,启动调试:

A brief discussion on several methods of debugging js code in VSCode

之后本项目的每次调试你只需要点击 Go Live 按钮并回到 VS Code 按下 F5 键就行了。

当然,chrome 调试 选项可能还是生效的,当你关掉 Live Server 打开的浏览器窗口时,可以尝试使用这个选项再次打开。

A brief discussion on several methods of debugging js code in VSCode

如果遇到端口号被占用的情况,可以点击调试旁边的齿轮来修改。

祝:

永无 BUG!


使用到的代码:

<!-- ./index.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="time">请点击下方按钮以获取当前时间</div>
    <button id="getTime">获取时间</button>
    <script ></script>
</body>
</html>
// ./js/index.js
let getTimeBtn = document.getElementById("getTime");
getTimeBtn.onclick = function (e) {
    let time = document.getElementById("time");
    let now = new Date().toLocaleString();
    time.innerHTML = now;
}
// ./.vscode/launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Chrome",
            "port": 9222,
            "request": "attach",
            "type": "pwa-chrome",
            "webRoot": "${workspaceFolder}"
        },
        {
            "type": "chrome",
            "request": "launch",
            "name": "chrome 调试",
            "url": "http://localhost:5500",
            "webRoot": "${workspaceFolder}"
        }
    ]
}

更多编程相关知识,请访问:编程课程!!

The above is the detailed content of A brief discussion on several methods of debugging js code in VSCode. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Visual Studio: Exploring Pricing and Licensing OptionsVisual Studio: Exploring Pricing and Licensing OptionsApr 13, 2025 am 12:03 AM

Visual Studio is available in three versions: Community Free Edition is for individuals and small teams, Professional Paid Edition is for professional developers and small and medium teams, and Enterprise Ultimate Edition is for large enterprises and complex projects.

Visual Studio's Value: Weighing the Cost Against Its BenefitsVisual Studio's Value: Weighing the Cost Against Its BenefitsApr 12, 2025 am 12:06 AM

VisualStudio is highly valuable in .NET development because it is powerful and comprehensive. Despite the high cost and resource consumption, the efficiency improvement and development experience it brings is significant. Community is ideal for individual developers and small teams; large enterprises are suitable for Professional or Enterprise.

Visual Studio's Availability: Which Editions Are Free?Visual Studio's Availability: Which Editions Are Free?Apr 10, 2025 am 09:44 AM

Free versions of VisualStudio include VisualStudioCommunity and VisualStudioCode. 1. VisualStudioCommunity is suitable for individual developers, open source projects and small teams. It is powerful and suitable for individual projects and learning programming. 2. VisualStudioCode is a lightweight code editor that supports multiple programming languages ​​and extensions. It has a fast startup speed and low resource usage, making it suitable for developers who need flexibility and scalability.

How to install Visual Studio for Windows 8?How to install Visual Studio for Windows 8?Apr 09, 2025 am 12:19 AM

The steps to install VisualStudio on Windows 8 are as follows: 1. Download the VisualStudioCommunity2019 installation package from the official Microsoft website. 2. Run the installer and select the required components. 3. It can be used after installation is completed. Be careful to select Windows 8-compatible components and make sure there is sufficient disk space and administrator rights.

Can my computer run VS Code?Can my computer run VS Code?Apr 08, 2025 am 12:16 AM

VSCode can run on most modern computers as long as the basic system requirements are met: 1. Operating system: Windows 7 and above, macOS 10.9 and above, Linux; 2. Processor: 1.6GHz or faster; 3. Memory: at least 2GB RAM (4GB or higher recommended); 4. Storage space: at least 200MB of available space. By optimizing settings and reducing extended usage, you can get a smooth user experience on low-configuration computers.

How do I make a program compatible with Windows 8?How do I make a program compatible with Windows 8?Apr 07, 2025 am 12:09 AM

To make the program run smoothly on Windows 8, the following steps are required: 1. Use compatibility mode, detect and enable this mode through code. 2. Adjust API calls and select the appropriate API according to the Windows version. 3. Perform performance optimization, try to avoid using compatibility mode, optimize API calls and use general controls.

Does VS Code work on Windows 8?Does VS Code work on Windows 8?Apr 06, 2025 am 12:13 AM

Yes,VSCodeiscompatiblewithWindows8.1)DownloadtheinstallerfromtheVSCodewebsiteandensurethelatest.NETFrameworkisinstalled.2)Installextensionsusingthecommandline,notingsomemayloadslower.3)Manageperformancebyclosingunnecessaryextensions,usinglightweightt

What is the difference between VS Code and Visual Studio?What is the difference between VS Code and Visual Studio?Apr 05, 2025 am 12:07 AM

VSCode is a lightweight code editor suitable for multiple languages ​​and extensions; VisualStudio is a powerful IDE mainly used for .NET development. 1.VSCode is based on Electron, supports cross-platform, and uses the Monaco editor. 2. VisualStudio uses Microsoft's independent technology stack to integrate debugging and compiler. 3.VSCode is suitable for simple tasks, and VisualStudio is suitable for large projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.