Debug JavaScript ใน Visual Studio (Windows) ทำได้โดยตั้งค่า Internet Options และใช้ Debugger ของ VS
ขั้นตอน
- เปิด Visual Studio → เลือก Project ใน Solution Explorer
- Right-click Project → Browse With... → ตั้ง Default Browser (Chrome/Edge/Firefox)
- เปิด Internet Options (Start → พิมพ์ "Internet Options")
- แท็บ Advanced → หากลุ่ม Browsing → Uncheck ทั้งสอง:
- ☐ Disable script debugging (Internet Explorer)
- ☐ Disable script debugging (Other)
- กด Apply → OK
- ตั้ง Breakpoints ในไฟล์ .js (คลิกขอบซ้ายหมายเลขบรรทัด)
- กด Debug (F5 หรือปุ่ม Play สีเขียว)
ภาพประกอบ
(รูปภาพต้นฉบับแสดง Internet Options Dialog: Advanced Tab, Browsing Section, Disable Script Debugging Unchecked)
หมายเหตุสำคัญ
- การตั้งค่านี้เกี่ยวกับ Internet Explorer / Legacy Edge (EdgeHTML) สำหรับ Modern Edge (Chromium), Chrome, Firefox ใช้ DevTools ของ Browser เองดีกว่า (F12)
- Visual Studio 2019/2022 รองรับ JavaScript Debugging ตรงใน IDE (Attach to Process, Chrome Debugging Extension)
- สำหรับ ASP.NET Core: ใช้
launchSettings.jsonกำหนด Browser และ Debug Profile
วิธีทันสมัยใน VS 2022+
- JavaScript Debug Terminal: Terminal → JavaScript Debug Terminal → รัน
npm start - Attach to Chrome: Debug → Attach to Process → Chrome (ต้องรัน Chrome ด้วย
--remote-debugging-port=9222) - Launch.json: กำหนด Config สำหรับ Chrome/Edge/Node.js
// .vscode/launch.json Example
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "https://localhost:3000",
"webRoot": "${workspaceFolder}"
}