You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code调试时nodemon无法监听TypeScript文件问题求助

Fix: Nodemon Not Listening to TypeScript Files in VS Code Debugging

Let's break down why your current setup isn't working and fix it step by step:

The Core Issue

Your launch.json is missing explicit instructions for nodemon to watch .ts files, and the -x argument is using a test echo command instead of invoking ts-node to run your TypeScript code. By default, nodemon only watches common JavaScript-related files unless told otherwise.

Corrected launch.json Configuration

Replace your current config with this updated version—each key change is explained below:

{
  "type": "node",
  "request": "launch",
  "name": "nodemon (TS)",
  "runtimeExecutable": "nodemon",
  "program": "${workspaceFolder}/index.ts",
  "restart": true,
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "args": [
    "--inspect-brk",
    "--ext", "ts",
    "-x", "ts-node"
  ],
  "timeout": 30000
}

Key Changes Explained

  • --ext ts: This tells nodemon to explicitly monitor changes in .ts files (add ,js if you also want to watch JavaScript files).
  • -x ts-node: Replaces your test echo command with ts-node, which compiles and runs your TypeScript file directly whenever nodemon detects a change.
  • Removed outFiles: Since we're using ts-node to run the TS file directly (no pre-compiled JS in dist needed for this workflow), this entry is no longer necessary. If you need to use pre-compiled JS, we can adjust the setup, but this aligns with your goal of using ts-node for on-the-fly compilation.

Prerequisites

Ensure these dev dependencies are installed in your project:

npm install -D nodemon ts-node typescript

How to Test

  1. Save the updated launch.json
  2. Start the "nodemon (TS)" debug configuration
  3. Make a change to index.ts and save it
  4. You should see nodemon restart the process, ts-node recompile your code, and the updated execution run in the integrated terminal.

Bonus: If You Prefer Pre-Compiling to dist

If you want to keep using the dist folder (compile TS to JS first), adjust the config like this:

{
  "type": "node",
  "request": "launch",
  "name": "nodemon (TS -> JS)",
  "runtimeExecutable": "nodemon",
  "program": "${workspaceFolder}/dist/index.js",
  "outFiles": ["${workspaceFolder}/dist/*.js"],
  "restart": true,
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "args": [
    "--inspect-brk",
    "--ext", "ts",
    "-x", "tsc && node"
  ],
  "timeout": 30000
}

This setup makes nodemon watch TS files, run tsc to compile to dist, then execute the resulting JS file.

内容的提问来源于stack exchange,提问作者user6643481

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:08:15