Home >Web Front-end >JS Tutorial >Using Nodemon and Watch in Node.js for Live Restarts

Using Nodemon and Watch in Node.js for Live Restarts

Lisa Kudrow
Lisa KudrowOriginal
2025-02-08 10:00:19422browse

Using Nodemon and Watch in Node.js for Live Restarts

Say goodbye to the cumbersomeness of repeated restarts in Node.js development! This article introduces two methods to help you improve development efficiency.

Core points

  • Efficient development with nodemon: We will introduce nodemon, a third-party Node.js module, which can effectively solve the problem of manually stopping and restarting the Node.js application after each code modification.
  • nodemon configuration options: We will cover various configuration options for nodemon, such as setting a specific path to monitor, ignoring a specific path, monitoring a specific file extension, adjusting restart delay, and setting environment variables.
  • Node.js --watch mode (for simple applications): For simple applications, if you are using Node.js 18.11 or later, you can use the experimental --watch option of Node.js . This built-in feature restarts the application whenever any imported file changes, providing a more direct alternative than nodemon without the need for additional third-party modules. However, it lacks the advanced control options available in nodemon.

If you have PHP development experience, you know you can always update the code and refresh the browser to test the changes. A web server like Apache or NGINX will receive your request for a PHP file and pass the contents to the PHP interpreter to execute the code. The server returns the generated output (usually HTML or JSON) to the calling browser. In other words, the code runs dynamically every time it is requested.

Node.js takes a different approach to web applications: Your JavaScript application is a web server. Running node index.js will initialize the application, load all modules and start a server that can respond to incoming requests. Changing the file does not affect the output of the application because it is already running in memory. To test for an update, you must close it with Ctrl | Cmd CCnode index.js> and run

again.

Node.js stop and restart processes can become very frustrating during debugging or during those rare focused hours, especially when making a lot of changes. Fortunately, there are two solutions:
  1. nodemon
  2. --watchNode.js
  3. Mode

nodemon

nodemon is a third-party Node.js module developed by JavaScript expert Remy Sharp. (He said you can pronounce it at will!)

You can install nodemon as a global module:
<code class="language-bash">npm install -g nodemon</code>

Then replace node with nodemon in the development startup command. For example, consider the following command:
<code class="language-bash">node --inspect index.js arg1 arg2</code>

The above command will now look like this:
<code class="language-bash">nodemon --inspect index.js arg1 arg2</code>

Your application will start as usual, but it will automatically restart when you edit and save the source file. No need to press Ctrl | Cmd Crs and run again, although you can type and press Enter

to force restart.

Note: nodemon is a server-side solution that does not refresh any browser tabs you point to the application. You can use tools like Browsersync or esbuild to achieve real-time reloading.

To get nodemon help, enter:

<code class="language-bash">npm install -g nodemon</code>

nodemon configuration

nodemon has its own set of command line parameters that take precedence over configurations elsewhere. You can also define configurations at:

  • Part of package.json in the "nodemonConfig" file of the project
  • Local nodemon.json Configuration file in the project directory, and/or
  • Global nodemon --config <file></file> configuration file used when running nodemon.json from the command line

The following parameters/settings are commonly used.

watch

nodemon monitors JavaScript files in the current working directory, but you can explicitly set a specific path using wildcards on the command line:

<code class="language-bash">node --inspect index.js arg1 arg2</code>

Or you can do this in the nodemon.json configuration file:

<code class="language-bash">nodemon --inspect index.js arg1 arg2</code>

ignore

Similarly, you can choose to ignore the path:

<code class="language-bash">nodemon --help</code>

Or you can do this in the nodemon.json configuration file:

<code class="language-bash">nodemon --watch lib1 config/*.json ./index.js</code>

ext

You can monitor specific files by their file extensions. For example, you can monitor js, cjs, mjs, json and njk template files like this:

<code class="language-json">{
  "watch": [
    "lib1",
    "config/*.json"
  ]
}</code>

Or you can do this in the nodemon.json configuration file:

<code class="language-bash">nodemon --ignore lib2 config/build.json ./index.js</code>

legacyWatch

In some environments, such as Docker containers that read files from mounted drives, file monitoring may fail. Switch to legacy monitoring mode Use polling to check if the file has been changed. From the command line:

<code class="language-json">{
  "ignore": [
    "lib2",
    "config/build.json"
  ]
}</code>

or in the nodemon.json configuration file:

<code class="language-bash">nodemon --ext "js,cjs,mjs,json,njk" ./index.js</code>

delay

nodemon waits for a second before triggering a restart. This is useful when you usually save many files at once. You can change the delay from the command line - for example, to five seconds:

<code class="language-json">{
  "ext": "js,cjs,mjs,json,njk"
}</code>

Or in the nodemon.json configuration file (note that this configuration uses milliseconds instead of seconds):

<code class="language-bash">nodemon --legacy-watch ./index.js</code>

verbose

Show detailed output log:

<code class="language-json">{
  "legacyWatch": true
}</code>

or in the nodemon.json configuration file:

<code class="language-bash">nodemon --delay 5 ./index.js</code>

env

Configuration file for setting a specific environment variable: nodemon.json

<code class="language-json">{
  "delay": 5000
}</code>
Other executables

Finally, you can use nodemon to launch applications written in other languages. For example, to start a perl script that automatically restarts:

<code class="language-bash">nodemon --verbose ./index.js</code>
You can also define a list of executables using its extension in the

configuration file: nodemon.json

<code class="language-json">{
  "verbose": true
}</code>
Advanced nodemon

If you need it, nodemon offers more advanced features:

  • Send a signal so that you can handle shutdown gracefully
  • Free event when nodemon's state changes
  • Transfer the output pipeline to other processes
  • Load nodemon as module into your project
  • Generate nodemon as child process, and
  • Use nodemon in Gulp and Grunt workflows.

Node.js --watch mode

If you have complex application startup requirements, nodemon is still the preferred tool. However, if you are using Node.js 18.11 (released late 2022) or later, it provides an experimental --watch option to restart your application without installing nodemon or any other third-party modules . For example, for the start command:

<code class="language-bash">npm install -g nodemon</code>

This will become:

<code class="language-bash">node --inspect index.js arg1 arg2</code>

Node.js will restart when any imported file changes. There are no other control options, so if it doesn't fit your project, consider using nodemon instead.

Summary

As your experience grows, you will find it increasingly useful to automatically restart Node.js applications. Please consider this as part of the development workflow in all projects.

The above is the detailed content of Using Nodemon and Watch in Node.js for Live Restarts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn