devops2025-04-15·7·118/348

nodemon 자동 재시작 안 됨 문제 해결

Fixing nodemon auto-restart issues including file watching configuration, ignore patterns, and signal handling.

nodemon 자동 재시작 안 됨 문제 해결

Introduction

nodemon is an essential development tool that automatically restarts your Node.js server when files change. When it stops working, it breaks the entire development workflow. This post covers the most common causes and fixes for nodemon restart issues.

Environment

node --version
# v20.11.0

npm list -g nodemon
# C:\Users\user\AppData\Roaming\npm\node_modules\nodemon
# +-- nodemon@3.0.2

Problem

nodemon starts but does not restart when files are modified:

nodemon server.js
[nodemon] 3.0.2
[nodemon] to restart due to changes; press rs
[nodemon] starting `node server.js`
Server running on port 3000

# ... edit server.js ...
# ... nothing happens, no restart!

Analysis

nodemon relies on file system watchers to detect changes. Common causes of watch failure:

  1. Large directories - node_modules, .git, dist
  2. Network drives - File system events not propagated
  3. IDE auto-save - Some editors do not trigger file system events
  4. Windows-specific issues - File system event limitations

The file watching flow:

File change → OS file system event → nodemon watcher → Restart

Solution

Solution 1: Configure watch paths and ignore patterns

// nodemon.json
{
  "watch": ["src", "config", "routes"],
  "ext": "js,json,ts",
  "ignore": [
    "node_modules/",
    "dist/",
    "build/",
    ".git/",
    "*.log",
    "coverage/"
  ],
  "exec": "node --watch src/server.js"
}

Solution 2: Use the --legacy-watch flag

# Force polling instead of native file system events
nodemon --legacy-watch server.js

# Or in nodemon.json
{
  "legacyWatch": true,
  "delay": 250
}

Solution 3: Configure in package.json

{
  "name": "my-app",
  "scripts": {
    "dev": "nodemon src/server.js",
    "dev:debug": "nodemon --inspect src/server.js"
  },
  "nodemonConfig": {
    "watch": ["src"],
    "ext": "js,ts,json",
    "ignore": ["src/**/*.spec.js", "src/**/*.test.js"]
  }
}

Solution 4: Windows-specific fix

# Install watchman for better file watching
choco install watchman

# Or use polling mode
nodemon --watch src --ext js --exec "node src/server.js" --legacy-watch
// Alternative: Use nodemon programmatically
const nodemon = require('nodemon');

nodemon({
  script: 'src/server.js',
  watch: ['src'],
  ext: 'js,json',
  ignore: ['node_modules', 'dist'],
  legacyWatch: process.platform === 'win32'
});

nodemon.on('start', () => {
  console.log('Server starting...');
});

nodemon.on('restart', (files) => {
  console.log('Restarting due to:', files);
});

nodemon.on('crash', () => {
  console.log('Server crashed, restarting...');
});

Solution 5: Debug nodemon

# Enable verbose logging
nodemon --verbose server.js

# Check what files are being watched
nodemon --dump

# Test file watching manually
nodemon --watch src --exec "echo File changed"

Solution 6: Signal-based restart

// Force restart from within the application
process.on('SIGUSR2', () => {
  console.log('Received SIGUSR2, restarting...');
  process.exit(0);
});

// Send restart signal
// kill -USR2 

Lessons Learned

  1. Use nodemon.json for complex configurations instead of CLI flags
  2. Set appropriate ignore patterns - Always ignore node_modules and dist
  3. Use legacy watch on Windows if native events do not work
  4. Configure watch paths specifically - Do not watch everything
  5. Use polling for network drives - Native events do not work on network file systems

This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.