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:
npm install -g nodemon
Then replace node with nodemon in the development startup command. For example, consider the following command:
node --inspect index.js arg1 arg2
The above command will now look like this:
nodemon --inspect index.js arg1 arg2
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:
npm install -g nodemon
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>
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:
node --inspect index.js arg1 arg2
Or you can do this in the nodemon.json
configuration file:
nodemon --inspect index.js arg1 arg2
Similarly, you can choose to ignore the path:
nodemon --help
Or you can do this in the nodemon.json
configuration file:
nodemon --watch lib1 config/*.json ./index.js
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" ] }
Or you can do this in the nodemon.json
configuration file:
nodemon --ignore lib2 config/build.json ./index.js
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" ] }
or in the nodemon.json
configuration file:
nodemon --ext "js,cjs,mjs,json,njk" ./index.js
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" }
Or in the nodemon.json
configuration file (note that this configuration uses milliseconds instead of seconds):
nodemon --legacy-watch ./index.js
Show detailed output log:
{ "legacyWatch": true }
or in the nodemon.json
configuration file:
nodemon --delay 5 ./index.js
Configuration file for setting a specific environment variable: nodemon.json
{ "delay": 5000 }
nodemon --verbose ./index.js
configuration file: nodemon.json
{ "verbose": true }
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
This will become:
node --inspect index.js arg1 arg2
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!