PM2配置文件自动重启失效及VS Code调试Node.js应用问题求助
Let's break down why your PM2 watch functionality isn't working and fix it step by step:
1. Check PM2 Version & Compatibility
First, outdated PM2 versions often have watch-mode bugs. Verify your version:
pm2 -v
If it’s not the latest, update with:
npm install pm2@latest -g
2. Validate Watch Paths & Ignore Rules
Your ignore_watch excludes node_modules and logs, but let’s rule out simple misconfigs:
- Confirm the files you’re editing are outside these ignored directories (changes inside
node_moduleswon’t trigger restarts by design). - Temporarily remove the
ignore_watchline to test if watch works without exclusions. If it does, your ignore pattern might have a typo or be overly broad. - Ensure
watch_options.followSymlinks: falseisn’t blocking detection of changes in symlinked files (if you use any).
3. Fix Conflict with --inspect-brk
Here’s a critical gotcha: your node_args: ["--inspect-brk"] pauses the Node.js process before executing code until a debugger attaches. If PM2 restarts the app but the process gets stuck waiting for your debugger, it can block watch triggers entirely.
Try two adjustments:
- If you don’t need to pause on startup, replace
--inspect-brkwith--inspect=9229(this lets the app run normally while listening for debugger connections). - If you need break-on-start behavior, attach your VS Code debugger immediately after PM2 starts the app—stuck processes prevent PM2 from detecting subsequent file changes.
4. Verify Ecosystem Config Syntax
A tiny syntax error can break the whole config. Test how PM2 interprets your file without starting the app:
pm2 start ecosystem.config.js --only development --dry-run
This will flag any invalid JSON/JS structure, missing commas, or typos.
5. Check PM2 Logs for Clues
PM2 logs often reveal why watch isn’t triggering. View your app’s logs with:
pm2 logs development
Look for lines about watch paths, file access permissions (PM2 might lack read access to your modified files), or process startup failures.
6. Isolate the Debugger
To confirm the debugger is the culprit, temporarily remove the node_args line from your ecosystem config, restart PM2, and test watch functionality. If it works, the issue is definitely tied to the debugger blocking the process.
Quick Tip for Your VS Code Debug Config
Your current attach-by-PID config works, but switching to port-based attachment (with --inspect instead of --inspect-brk) is often more reliable:
{ "type": "node", "request": "attach", "name": "Attach to PM2", "port": 9229 }
Once you work through these steps, your watch functionality should start triggering restarts as expected. Let me know if you hit any roadblocks!
内容的提问来源于stack exchange,提问作者samsap

