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

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.
- Open your Node.js project in VS Code.
- Go to the Run and Debug view (Ctrl Shift D or Cmd Shift D).
- Click create a launch.json file if you don't have one.
- Select Node.js as the environment.
This generates a default launch.json
like:

{ "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.

{ "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 specifynpm
. -
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:
RunCtrl Shift P
→ "Debug: Toggle Auto Attach" → choose "Always".
Now anynode
script started from the integrated terminal will auto-attach.
Troubleshooting common issues
Breakpoints not hit?
Check that theprogram
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中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

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

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

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

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

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

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

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

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

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

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

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