Home >Web Front-end >JS Tutorial >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
--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
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:
--watch
Node.js 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
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 has its own set of command line parameters that take precedence over configurations elsewhere. You can also define configurations at:
package.json
in the "nodemonConfig"
file of the project nodemon.json
Configuration file in the project directory, and/or nodemon --config <file></file>
configuration file used when running nodemon.json
from the command line The following parameters/settings are commonly used.
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>
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>
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>
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>
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>
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>
Configuration file for setting a specific environment variable: nodemon.json
<code class="language-json">{ "delay": 5000 }</code>Other executables
<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 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.
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!