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
Release: 2025-02-08 10:00:19
Original
399 people have browsed it

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:
npm install -g nodemon
Copy after login
Copy after login
Copy after login

Then replace node with nodemon in the development startup command. For example, consider the following command:
node --inspect index.js arg1 arg2
Copy after login
Copy after login
Copy after login

The above command will now look like this:
nodemon --inspect index.js arg1 arg2
Copy after login
Copy after login

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:

npm install -g nodemon
Copy after login
Copy after login
Copy after login

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> 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:

node --inspect index.js arg1 arg2
Copy after login
Copy after login
Copy after login

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

nodemon --inspect index.js arg1 arg2
Copy after login
Copy after login

ignore

Similarly, you can choose to ignore the path:

nodemon --help
Copy after login

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

nodemon --watch lib1 config/*.json ./index.js
Copy after login

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:

{
  "watch": [
    "lib1",
    "config/*.json"
  ]
}
Copy after login

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

nodemon --ignore lib2 config/build.json ./index.js
Copy after login

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:

{
  "ignore": [
    "lib2",
    "config/build.json"
  ]
}
Copy after login

or in the nodemon.json configuration file:

nodemon --ext "js,cjs,mjs,json,njk" ./index.js
Copy after login

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:

{
  "ext": "js,cjs,mjs,json,njk"
}
Copy after login

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

nodemon --legacy-watch ./index.js
Copy after login

verbose

Show detailed output log:

{
  "legacyWatch": true
}
Copy after login

or in the nodemon.json configuration file:

nodemon --delay 5 ./index.js
Copy after login

env

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

{
  "delay": 5000
}
Copy after login
Other executables

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

nodemon --verbose ./index.js
Copy after login
You can also define a list of executables using its extension in the

configuration file: nodemon.json

{
  "verbose": true
}
Copy after login
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:

npm install -g nodemon
Copy after login
Copy after login
Copy after login

This will become:

node --inspect index.js arg1 arg2
Copy after login
Copy after login
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template