VS Code调试时nodemon无法监听TypeScript文件问题求助
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.tsfiles (add,jsif you also want to watch JavaScript files).-x ts-node: Replaces your test echo command withts-node, which compiles and runs your TypeScript file directly whenever nodemon detects a change.- Removed
outFiles: Since we're usingts-nodeto run the TS file directly (no pre-compiled JS indistneeded 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 usingts-nodefor on-the-fly compilation.
Prerequisites
Ensure these dev dependencies are installed in your project:
npm install -D nodemon ts-node typescript
How to Test
- Save the updated
launch.json - Start the "nodemon (TS)" debug configuration
- Make a change to
index.tsand save it - You should see nodemon restart the process,
ts-noderecompile 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

