What is a Nodemon?

Nodemon is a tool that automatically restarts node applications. When the monitored files or files in the monitored directory are modified, the application is automatically restarted.

Using Nodemon does not require any changes to the application’s own code. The Nodemon can be regarded as the execution container of node. To use Nodemon, you only need to change Node to Nodemon when executing the code.

How to install?

Git Clone code directly, or use NPM installation (recommended) :

npm install -g nodemon

Nodemon will be installed globally and added to the system path.

You can also install it locally as a development dependency:

npm install --save-dev nodemon

If it is installed on the local PC, it cannot be directly used through the system path. It can be started using NPM script (for example, NPM start) or using NPX Nodemon.

How to use it?

Nodemon as the container for your app, you can pass in any parameters you want to your app, just like you would pass parameters:

nodemon [your node app]

You can use -h(or -help) to see the options that can be passed to Nodemon:

nodemon -h

Using Nodemon is very simple. If your application receives parameters host and port to start the application, you can write it like this:

nodemon ./server.js localhost 8080

Any output from the above command will be prefixed with [nodemon], and any output from your app, including errors, will be printed as expected.

If script.js is not provided, Nodemon looks for the package.json file and, if it exists, executes using the value specified by the main attribute in that file.

You can also pass the inspect flag via the command line:

nodemon --inspect ./server.js 80

If you have a package.json file in your application, you can also omit the main entry of script and Nodemon will read the value specified by the main property in the package.json file.

Nodemon will also search for the scripts.start property in package.json.

View FAQs or issues

Automatic restart

Nodemon originally only supports restarting running processes, such as Web services, but now also supports restarting applications after they exit. If your app exits, Nodemon continues to listen to the specified directory and automatically restarts when changes occur.

Manually restart

When Nodemon is running, if you want to restart the application, you do not need to exit and restart Nodemon. You can enter RS and press Enter, and Nodemon will restart your application.

The configuration file

Local and global configuration files are supported. The configuration file is usually named nodemon.json and can be placed in the current directory or your root directory. If a configuration file with another name is configured, you can specify it by using the *- -config * option.

It is special in that command line arguments always override configuration file arguments, with the following priorities:

  • Command line input parameters
  • Local configuration file
  • Global configuration file

Configuration files can specify any command-line arguments in JSON key-value pairs, for example:

{
	"verbose": true."ignore": ["*.test.js"."fixtures/*"]."execMap": {
		"rb": "ruby"."pde": "processing --sketch={{pwd}} --run"}}Copy the code

I generally take the above nodemon.json as my global configuration. The above configuration can support Ruby files and processing files and execute nodemon Demo. pde.

See more options

package.json

If you want to centrally manage package configuration in your project, Nodemon supports configuration in package.json files. In package.json, the configuration is defined under the nodemonConfig property in the same format as a separate configuration file, such as the following package.json configuration:

{
	"name": "nodemon"."homepage": "http://nodemon.io"."...": "... other standard package.json value"."nodemonConfig": {
		"ignore": ["test/*"."docs/*"]."delay": "2500"}}Copy the code

Note that if a configuration file is specified by *- -config * or a local nodemon.json is configured, all configurations in package.json are ignored.

This section requires more detailed documentation, but for now you can view the configurable items/via nodemon — -help

Use Nodemon as a module

View usage separately open article translation

Use Nodemon as a child process

View usage separately open article translation

Run non-Node scripts

Nodemon can be used to execute and listen on other program projects. If nodemon.json does not exist, nodemon will read the suffix of the executing script and listen for it instead of the.js file:

nodemon --exec "python -v" ./app.py

Nodemon will now run app.py in verbose mode based on Python (note that if you don’t need parameters, you don’t need the parts in quotes) and require a new file or file modification with a.py extension.

The default executable file

Using the Nodemon. json configuration file, you can define your own default execution through the execMap property. This property is useful when you are using a language that is not supported by Nodemon by default.

To increase Nodemon’s support for.pl extension (Perl) files, you can configure it as follows:

{ 
	"execMap": {
		"pl": "perl"}}Copy the code

Nodemon now knows to use Perl as the executable environment by executing the following code:

nodemon script.pl

It is recommended to use global configuration to add your own execMap options. Of course, if you find any common options missing, you can modify default.js and send a pull request, which Nodemon can support by default after confirmation.

Listening for multiple directories

Nodemon listens to the current working directory by default. If you want to control the listening range yourself, you can add the specified path with the *- – watch* argument:

nodemon --watch app --watch libs app/server.js

When the APP or LIBS is modified, the restart will be triggered. Nodemon traverses subdirectories by default, so there is no need to specify a subdirectory.

Do not use wildcards to pass multiple directories, such as — watch. /lib/*. This will not work. You need to use — watch to specify each directory you want to listen on.

Specifies the extension of the listen object

Nodemon extension have default listening object. Js /. MJS/coffee /. Litcoffee /. Json. If you use the *- -exec option and listen on app.py, Nodemon will listen on files with the.py extension. You can also use -e or — ext* to specify your own list, as follows:

nodemon -e js,pug

Nodemon will now also listen for files with.js or.pug extensions.

Ignore files

By default, nodemon restarts the application when the. Js file is modified. Sometimes, you might want to ignore certain file/directory/file name patterns (matching file names) to prevent Nodemon from restarting the app prematurely.

To ignore a specified file, use *- -ignore * :

nodemon --ignore lib/ --ignore tests/

Or specify that a file is ignored

nodemon --ignore lib/app.js

If pattern is specified, matching files can be ignored (but be sure to include arguments in quotes) :

nodemon --ignore 'lib/*.js'

Git /node_modules/bower_components/.nyc_output/coverage/.saas-cache. You can add your own ignore patterns to this list. If you really want to listen on folders like node_modules, you need to override the underlying default ignore list.

The application is not restarted

In some network environments (such as a container running Nodemon reading from a mounted driver), you need to use legacyWatch: true to start Chokidar polling:

From the command line, use the argument — legacy-watch or -l:

nodemon -L

This parameter is reserved because it will poll all the files it can find.

Delay to restart

In some scenarios, you may want to wait for a certain number of file changes before restarting the application. By default, 1s events are reserved for new file modification. If you upload a certain number of files that will take several seconds, it may cause your application to restart frequently.

Use the -delay option to delay the restart after the specified time:

nodemon --delay 10 server.js

The delay time can be accurate to the millisecond. Such as:

Nodemon -- 2.5 server delay. Js

Or specify a time unit

nodemon --delay 2500ms server.js

The delay parameter specifies how many seconds (or milliseconds) to delay restarting. Therefore, Nodemon will restart at the delay specified after the last file modification.

If you specify the delay time in nodemon.json, the default unit is ms. The following two ways are equivalent:

Nodemon –delay 2.5 {“delay”: “2500”}

Elegant overloaded scripts

Nodemon can send the signal you specify to your application.

nodemon --signal SIGHUP server.js

Your application can use signals as follows:

process.once("SIGHUP", () => {
	reloadSomeConfiguration();
})
Copy the code

Note that Nodemon will send signals to each process in the process tree.

If you use a cluster, each worker(like the master) gets a signal. If you want to prevent all workers from receiving a SIGHUP, a common way is to capture the SIGHUP in the master and broadcast the SIGTERM to all workers so that all workers are guaranteed to ignore the SIGHUP.

if (cluster.isMaster) {
	process.on("SIGHUP", () = > {for(const worker of Object.values(cluster.workers)) {
			workder.process.kill("SIGTERM")}}}else {
	process.on("SIGHUP", () => {})}Copy the code

Control the exit of the script

Nodemon sends a kill signal to your app when it listens for file updates. If you need to do some cleanup when your script exits, you can catch the kill signal and handle it yourself.

In the following example, listen for SIGUSR2 once (the signal Nodemon uses to restart), run the cleanup process, and then kill itself so that Nodemon can continue control.

process.once('SIGUSR2', () => {
	gracefulShutdown((a)= > {
		process.kill(process.pid, 'SIGUSR2'); })})Copy the code

Note that the process.kill method needs to be called only once when the exit job is complete. Use case

The event is triggered when the nodemon status is changed

If you want to send a notification when Nodemon restarts, or trigger an action when an event occurs, you can either raise pr for Nodemon or add event Actions to nodemon.json file.

For example, in the Mac system, when nodemon restarts, it triggers a notification, which can be defined as follows:

{
	"events": {
		"restart": "osascript - e 'display notification \"app restarted\"' with title \"nodemon\"'"}}Copy the code

Detailed status list. You can bind all the states and information.

Output information elsewhere

nodemon({
	script:... .stdout: false // Critical, set to false, do not output to console
}).on('readable', () = > {this.stdout.pipe(fs.createWriteStream('output.txt'));
	this.stderr.pipe(fs.createWriteStream('err.txt'));
})
Copy the code

Work with GLUP

Using the gulp-Nodemon plugin, you can integrate Nodemon into your gulp workflow.

Work with Grunt

Use the Gunt-Nodemon plugin to integrate Nodemon into your Grunt workflow.

How do you pronounce “nodemon”?

How should “nodemon” be pronounced? Node-mon, no-demon, or Node-e-mon?

Well, I’ve been asked this question many times before. I’m glad you have such doubts. There are also bets on which way to pronounce it is correct.

The answer is simple, but it may disappoint you. I won’t tell you how I say it. How you pronounce it is up to you. All answers are correct

Design principles

  • As few flags as possible
  • Universal platform
  • As few features as possible
  • Based on Nodemon independence (I think it means that even without Nodemon, it can run independently)
  • Provides all CLI functions as apis
  • Test coverage of 100%

Nodemon is imperfect and the CLI parameters are too loose, which I’m not happy about, but maybe one day there will be one or two less.

FAQ

Check out the FAQ and ask questions that you find helpful.