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

Nodemon修改index.js文件不重启的原因及解决方法

Why Nodemon Doesn't Restart When You Modify index.js

Let's break down what's happening here. Your current dev script looks like this:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node index.js",
  "dev": "nodemon --watch app index.js"
}

You've noticed that when you edit index.js (your root entry file), Nodemon doesn't trigger a restart—even though it correctly watches changes in the app folder.

The Root Cause

The --watch app flag is the culprit here. When you specify a --watch path with Nodemon, it overrides the default behavior (which is to watch all relevant files in the current directory and subdirectories). Instead, it only monitors the exact paths you've explicitly listed. Since index.js is in the root directory and not included in the --watch app target, Nodemon ignores changes to it entirely.

Fixes That Work

There are two straightforward ways to fix this:

1. Use Nodemon's Default Watching Behavior

As @Pedro Filipe suggested, simply remove the --watch app flag:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node index.js",
  "dev": "nodemon index.js"
}

Now Nodemon will go back to its default behavior: it watches all supported file types in your root directory (including index.js) and all subdirectories (like app). Any changes to these files will trigger a restart.

2. Explicitly Watch Both Targets

If you want to be more restrictive (only watch app and index.js, not other root files like package.json), you can list multiple --watch targets:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node index.js",
  "dev": "nodemon --watch app --watch index.js"
}

This tells Nodemon to monitor both the app directory and the root index.js file—changes to either will trigger a restart.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 14:52:47