国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
nodemon configuration
watch
ignore
ext
legacyWatch
delay
verbose
env
Node.js --watch mode
Summary
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

Feb 08, 2025 am 10:00 AM

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

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

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

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

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

nodemon --inspect index.js arg1 arg2

ignore

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

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"
  ]
}

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

nodemon --ignore lib2 config/build.json ./index.js

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"
  ]
}

or in the nodemon.json configuration file:

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

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"
}

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

nodemon --legacy-watch ./index.js

verbose

Show detailed output log:

{
  "legacyWatch": true
}

or in the nodemon.json configuration file:

nodemon --delay 5 ./index.js

env

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

{
  "delay": 5000
}
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
You can also define a list of executables using its extension in the

configuration file: nodemon.json

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

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.

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java vs. JavaScript: Clearing Up the Confusion Java vs. JavaScript: Clearing Up the Confusion Jun 20, 2025 am 12:27 AM

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

Javascript Comments: short explanation Javascript Comments: short explanation Jun 19, 2025 am 12:40 AM

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

How to work with dates and times in js? How to work with dates and times in js? Jul 01, 2025 am 01:27 AM

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

JavaScript vs. Java: A Comprehensive Comparison for Developers JavaScript vs. Java: A Comprehensive Comparison for Developers Jun 20, 2025 am 12:21 AM

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

Why should you place  tags at the bottom of the ? Why should you place tags at the bottom of the ? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript: Exploring Data Types for Efficient Coding JavaScript: Exploring Data Types for Efficient Coding Jun 20, 2025 am 12:46 AM

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

What is event bubbling and capturing in the DOM? What is event bubbling and capturing in the DOM? Jul 02, 2025 am 01:19 AM

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

What's the Difference Between Java and JavaScript? What's the Difference Between Java and JavaScript? Jun 17, 2025 am 09:17 AM

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.

See all articles