目錄
Set up a debug configuration
Common debugging scenarios
Launch and debug a script
Attach to a running Node.js process
Debug npm scripts
Tips for effective debugging
Troubleshooting common issues
首頁 開發工具 VSCode 如何在VS代碼中調試Node.js應用程序

如何在VS代碼中調試Node.js應用程序

Sep 20, 2025 am 04:04 AM
node.js vs code

配置launch.json 文件以設置調試環境,確保program 字段指向主入口文件;2. 使用launch 模式直接啟動腳本並傳入args 和env 參數;3. 使用attach 模式連接已通過node --inspect 啟動的運行中進程;4. 調試npm 腳本時設置runtimeExecutable 為npm 並啟用integratedTerminal;5. 通過設置斷點、條件斷點、變量檢查、表達式求值及啟用Auto Attach 提升調試效率;6. 若遇問題,檢查路徑、啟動參數、端口匹配、sourceMaps 配置並重啟調試會話,最終實現無縫調試。

How to debug Node.js applications in VS Code

Debugging Node.js applications in VS Code is straightforward once you set up the environment correctly. The built-in debugger in VS Code provides a powerful way to inspect variables, set breakpoints, step through code, and evaluate expressions—all without leaving your editor.

How to debug Node.js applications in VS Code

Set up a debug configuration

To start debugging, you need a launch configuration. VS Code uses a launch.json file inside a .vscode folder in your project root.

  1. Open your Node.js project in VS Code.
  2. Go to the Run and Debug view (Ctrl Shift D or Cmd Shift D).
  3. Click create a launch.json file if you don't have one.
  4. Select Node.js as the environment.

This generates a default launch.json like:

How to debug Node.js applications in VS Code
 {
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Index",
      "program": "${workspaceFolder}/index.js"
    }
  ]
}

Make sure the program field points to your main entry file (eg, app.js , server.js , etc.).

Common debugging scenarios

Launch and debug a script

Use the launch mode to start your app directly from the debugger.

How to debug Node.js applications in VS Code
 {
  "type": "node",
  "request": "launch",
  "name": "Start Server",
  "program": "${workspaceFolder}/server.js",
  "args": ["--port", "3000"],
  "env": {
    "NODE_ENV": "development"
  }
}
  • args : Pass command-line arguments.
  • env : Define environment variables.

Start the debug session by selecting this configuration and pressing F5.

Attach to a running Node.js process

If your app is already running (eg, started from terminal), use attach mode.

First, start your app with inspection enabled:

 node --inspect server.js
# or for breaking on first line:
node --inspect-brk server.js

Then use this launch.json configuration:

 {
  "type": "node",
  "request": "attach",
  "name": "Attach to Port",
  "port": 9229
}

VS Code will connect to the Node.js process on port 9229 (default debugging port). You can set breakpoints and inspect runtime state.

Debug npm scripts

Instead of calling node directly, you can debug scripts defined in package.json .

 {
  "type": "node",
  "request": "launch",
  "name": "Debug Start",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run", "start"],
  "console": "integratedTerminal",
  "port": 9229
}
  • Use runtimeExecutable to specify npm .
  • console: "integratedTerminal" ensures output appears in the terminal, which is useful for interactive apps.

Make sure your start script runs Node with --inspect if needed.

Tips for effective debugging

  • Set breakpoints by clicking the left margin next to line numbers or pressing F9.
  • Use conditional breakpoints by right-clicking a breakpoint and setting a condition (eg, user.id === 123 ).
  • Inspect variables in the VARIABLES pane or WATCH window.
  • Use Debug Console to evaluate expressions or call functions during a breakpoint.
  • Enable Auto Attach for simple cases:
    Run Ctrl Shift P → "Debug: Toggle Auto Attach" → choose "Always".
    Now any node script started from the integrated terminal will auto-attach.

Troubleshooting common issues

  • Breakpoints not hit?
    Check that the program path is correct and the file is actually being executed. Use --inspect-brk to pause on startup.

  • Can't connect when attaching?
    Ensure the Node.js process was started with --inspect and that the port matches.

  • Source maps not working?
    If using TypeScript or bundlers, add "sourceMaps": true and point to generated files:

     "resolveSourceMapLocations": [
      "${workspaceFolder}/**",
      "!**/node_modules/**"
    ]
  • Changes not reflected?
    The debugger runs a specific version of your code. Restart the debug session after saving changes unless using a file watcher.

  • Basically, with the right launch.json setup, VS Code gives you a seamless debugging experience for most Node.js apps—whether launching directly, attaching to processes, or debugging through npm scripts.

    以上是如何在VS代碼中調試Node.js應用程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

運行與代碼的系統要求是什麼? 運行與代碼的系統要求是什麼? Jul 15, 2025 am 01:56 AM

VisualStudioCode可在較低配置硬件上流暢運行。其最低配置要求為:64位Windows10或更高版本、macOS10.13或更高版本、現代主流Linux發行版;處理器需1.6GHz及以上(推薦雙核);至少4GB內存(處理大型項目建議8GB);約500MB可用磁盤空間;分辨率1024x768及以上。推薦配置包括8–16GB內存、SSD存儲、多核CPU及更大屏幕或多顯示器,以提升性能和開發體驗。對於低配設備優化建議包括:禁用不必要的擴展、關閉自動保存、使用內置終端、避免直接打開大文件夾、

如何在node.js中提出HTTP請求? 如何在node.js中提出HTTP請求? Jul 13, 2025 am 02:18 AM

在Node.js中發起HTTP請求有三種常用方式:使用內置模塊、axios和node-fetch。 1.使用內置的http/https模塊無需依賴,適合基礎場景,但需手動處理數據拼接和錯誤監聽,例如用https.get()獲取數據或通過.write()發送POST請求;2.axios是基於Promise的第三方庫,語法簡潔且功能強大,支持async/await、自動JSON轉換、攔截器等,推薦用於簡化異步請求操作;3.node-fetch提供類似瀏覽器fetch的風格,基於Promise且語法簡單

如何在VS代碼中配置任務參數? 如何在VS代碼中配置任務參數? Jul 12, 2025 am 01:41 AM

topAssArgumentsInvScodEtasks,configuretheargsarrayIntasks.json.1.StructureCommandPartsbySebyParationTheCommandAnditSargumentsArgumentsIntocommandargs,例如,命令“:”:“ python”:“ python”和“ python”和“ python”和“ args”和“

如何在VS代碼中使用時間軸視圖作為本地歷史記錄 如何在VS代碼中使用時間軸視圖作為本地歷史記錄 Aug 20, 2025 am 07:22 AM

TheTimelineviewinVSCodeprovidesaccesstolocalhistoryfortrackingandrestoringfilechangeswithoutGit;1)OpenafileandclickitinFileExplorertorevealtheTimeline;2)Viewtimestampedlocalhistoryentriescreatedonsave;3)Clickanentrytopreviewchangesindiffeditor;4)Righ

VS代碼是否可以免費使用,即使是出於商業目的? VS代碼是否可以免費使用,即使是出於商業目的? Jul 24, 2025 am 02:19 AM

是的,VSCode完全免費,包括商業用途。它由微軟開發,採用MIT許可證發布,允許用戶自由使用、安裝、修改和分發,無需支付費用或擔心授權限制;具體來說:1.可在任意設備上安裝使用,無時間限製或功能付費牆;2.商業用戶無需回饋項目或披露使用情況;3.沒有官方付費版本或高級功能,所有工具如調試器和Git集成均免費提供;4.第三方擴展可能包含付費功能,但非官方提供;5.用戶可修改並重新分發VSCode,但須遵守許可證要求,保留原始版權信息;6.若用於企業環境,建議法律團隊確認合規性;對於絕大多數用戶而

如何將node.js應用程序部署到IIS? 如何將node.js應用程序部署到IIS? Aug 30, 2025 am 01:37 AM

答案是使用iisnode在IIS上部署Node.js應用。首先啟用IIS及所需組件,安裝ARR、URLRewrite和iisnode模塊;準備應用並確保監聽process.env.PORT;在IIS中創建站點,設置應用池為“無託管代碼”,配置web.config文件將請求重寫至app.js;最後通過瀏覽器測試並檢查iisnode日誌排查錯誤,實現IIS作為反向代理運行Node.js應用。

如何在VSCODE中使用Google Cloud平台? 如何在VSCODE中使用Google Cloud平台? Aug 05, 2025 am 07:38 AM

安裝GoogleCloudCode擴展以在VSCode中開發、調試和部署GCP應用;2.安裝並配置GoogleCloudSDK(gcloudCLI),通過gcloudauthlogin認證並設置項目;3.使用CloudCode功能創建新應用、部署到CloudRun、本地調試Kubernetes應用並查看日誌;4.可選安裝Docker、YAML、Remote-SSH等擴展增強開發體驗;5.通過命令面板部署應用到CloudRun,選擇區域和服務參數完成部署,最終獲得應用URL,整個過程需結合Clou

如何將VS代碼與Angular一起使用? 如何將VS代碼與Angular一起使用? Jul 13, 2025 am 01:39 AM

使用VSCode開發Angular項目高效便捷,關鍵在於正確配置環境。首先安裝Node.js和npm;其次通過npm全局安裝AngularCLI以便創建項目和生成組件;然後在VSCode中安裝AngularLanguageService、Prettier或ESLint及調試插件等擴展提升開發體驗;接著使用ngnew命令創建項目並在VSCode中打開;利用IntelliSense實現代碼自動補全,通過Ctrl P快速導航文件,使用F12跳轉定義;運行ngserve啟動開發服務器並啟用自動重載;配置

See all articles