#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Node.js & Express

Nodemon

Nodemon is a development tool that watches your Node.js project files and automatically restarts your server whenever you save a change. Without it, every edit to an Express route means stopping the server with Ctrl+C and running node index.js again. With it, you save the file and the new code is running a moment later.

Installing nodemon

You can install nodemon globally, but adding it as a development dependency of each project is usually better. That way everyone who clones the project gets the same version, and it is not shipped with your production dependencies.

# add nodemon to this project only, as a dev dependency
npm install --save-dev nodemon

# package.json
{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
}

# run the development server
npm run dev

npm scripts automatically find tools installed in node_modules/.bin, so "dev": "nodemon index.js" works without a global install. You can also run it once with npx nodemon index.js.

What you see when it runs

Nodemon prints a few lines in yellow and green, telling you which extensions and folders it is watching, and then starts your app. When you save a watched file, it prints "restarting due to changes..." and starts a fresh process. If your app crashes, nodemon does not exit; it shows "app crashed - waiting for file changes before starting...", so you can fix the error and save again.

Typing rs and pressing Enter in that terminal forces a manual restart, which is handy after changing something nodemon does not watch, like an environment variable.

Configuring what it watches

By default nodemon watches files with extensions such as .js, .mjs, .cjs and .json in the current folder, and ignores node_modules. You can change this with command-line flags or a nodemon.json file in the project root:

  • "watch": ["src"] to watch only one folder.
  • "ext": "js,json,ejs" to include template files.
  • "ignore": ["data/*.json", "tests/"] to skip files your app writes to.
  • "delay": 500 to wait briefly when many files change at once.

The endless restart loop

A classic problem: your app writes to a JSON file, for example saving users to data/users.json. Nodemon sees that file change and restarts the server, which may write the file again, causing a restart loop, or it simply restarts in the middle of a request and loses data. The fix is to add that file or folder to ignore.

Another frequent issue is "address already in use" after a restart. This usually means a different server is still running on that port in another terminal. Close the old one, or change the port.

Development only, never production

Nodemon is for your own machine. In production, an app should not restart because a file changed, and you want a process manager or your hosting platform to handle crashes and restarts. That is why the example keeps "start": "node index.js" for production and uses a separate dev script.

The built-in alternative

Recent Node versions, including Node 20 and later, have a --watch flag: node --watch index.js. It covers the basic case of restarting on save without any extra package. Nodemon remains popular because of its configuration options, but for small projects the built-in flag may be all you need.

Setup in short: install as a dev dependency, add a dev script, ignore files your app writes, and keep plain node for production.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course