Installation

Either through cloning with git or by using npm (the recommended way):


And nodetháng will be installed globally khổng lồ your system path.

Bạn đang xem: Tại sao bạn nên bắt đầu sử dụng nodemon để phát triển các ứng dụng node

You can also install nodemon as a development dependency:


For CLI options, use the -h (or --help) argument:


Using nodetháng is simple, if my application accepted a host and port as the arguments, I would start it as so:


Any output from this script is prefixed with , otherwise all output from your application, errors included, will be echoed out as expected.

You can also pass the inspect flag khổng lồ node through the command line as you would normally:


nodetháng --inspect ./hệ thống.js 80
If you have sầu a package.json tệp tin for your app, you can omit the main script entirely và nodetháng will read the package.json for the main property & use that value as the app (ref).

nodetháng will also search for the scripts.start property in package.json (as of nodetháng 1.1.x).

Also check out the FAQ or issues for nodemon.

Automatic re-running

nodetháng was originally written khổng lồ restart hanging processes such as web servers, but now supports apps that cleanly exit. If your script exits cleanly, nodetháng will continue to lớn monitor the directory (or directories) và restart the script if there are any changes.

Manual restarting

Whilst nodemon is running, if you need to manually restart your application, instead of stopping & restart nodetháng, you can type rs with a carriage return, và nodetháng will restart your process.

Config files

nodemon supports local & global configuration files. These are usually named nodetháng.json và can be located in the current working directory or in your home page directory. An alternative local configuration file can be specified with the --config option.

The specificity is as follows, so that a command line argument will always override the config tệp tin settings:

comm& line argumentslocal configglobal config

A config file can take any of the comm& line arguments as JSON key values, for example:


"verbose": true, "ignore": <"*.chạy thử.js", "fixtures/*">, "execMap": "rb": "ruby", "pde": "processing --sketch=pwd --run"
The above nodetháng.json file might be my global config so that I have sầu support for ruby files và processing files, & I can run nodemon test.pde and nodetháng will automatically know how lớn run the script even though out of the box support for processing scripts.

A further example of options can be seen in sample-nodemon.md

package.json

If you want to keep all your package configurations in one place, nodemon supports using package.json for configuration.Specify the config in the same format as you would for a config file but under nodemonConfig in the package.json file, for example, take the following package.json:


"name": "nodemon", "homepage": "http://nodetháng.io", "...": "... other standard package.json values", "nodemonConfig": "ignore": <"test/*", "docs/*">, "delay": 2500
lưu ý that if you specify a --config tệp tin or provide a local nodetháng.json any package.json config is ignored.

This section needs better documentation, but for now you can also see nodetháng --help config (also here).

Using nodetháng as a module

Please see doc/requireable.md

Using nodetháng as child process

Please see doc/events.md

Running non-node scripts

nodemon can also be used khổng lồ exedễ thương & monitor other programs. nodemon will read the tệp tin extension of the script being run and monitor that extension instead of .js if there"s no nodetháng.json:


nodetháng --exec "python thả -v" ./app.py
Now nodemon will run phầm mềm.py with pythanh mảnh in verbose mode (note that if you"re not passing args lớn the exec program, you don"t need the quotes), and look for new or modified files with the .py extension.

Default executables

Using the nodemon.json config file, you can define your own mặc định executables using the execMap property. This is particularly useful if you"re working with a language that isn"t supported by mặc định by nodetháng.

To add tư vấn for nodetháng khổng lồ know about the .pl extension (for Perl), the nodetháng.json tệp tin would add:


"execMap": "pl": "perl"
Now running the following, nodemon will know khổng lồ use perl as the executable:


nodetháng script.pl
It"s generally recommended lớn use the global nodemon.json lớn add your own execMap options. However, if there"s a comtháng mặc định that"s missing, this can be merged in khổng lồ the project so that nodemon supports it by default, by changing mặc định.js & sending a pull request.

Monitoring multiple directories

By mặc định nodemon monitors the current working directory. If you want to take control of that option, use the --watch option lớn add specific paths:


nodemon --watch app --watch libs app/server.js
Now nodetháng will only restart if there are changes in the ./phầm mềm or ./libs directory. By mặc định nodetháng will traverse sub-directories, so there"s no need in explicitly including sub-directories.

Don"t use unix globbing to lớn pass multiple directories, e.g --watch ./lib/*, it won"t work. You need a --watch flag per directory watched.

Specifying extension watch list

By mặc định, nodemon looks for files with the .js, .mjs, .coffee, .litcoffee, and .json extensions. If you use the --exec option and monitor app.py nodemon will monitor files with the extension of .py. However, you can specify your own các mục with the -e (or --ext) switch lượt thích so:


nodetháng -e js,pug
Now nodemon will restart on any changes lớn files in the directory (or subdirectories) with the extensions .js, .pug.

Ignoring files

By default, nodetháng will only restart when a .js JavaScript tệp tin changes. In some cases you will want to ignore some specific files, directories or tệp tin patterns, to lớn prevent nodemon from prematurely restarting your application.

Xem thêm: X Em Là Ai Tập 5 Vietsub + Thuyết Minh Full Hd, Động Phim, Xem Phim Em Là Ai

This can be done via the command line:


nodemon --ignore lib/ --ignore tests/
Or specific files can be ignored:


nodemon --ignore lib/tiện ích.js
Patterns can also be ignored (but be sure to quote the arguments):


nodetháng --ignore "lib/*.js"
cảnh báo that by mặc định, nodetháng will ignore the .git, node_modules, bower_components, .nyc_output, coverage và .sass-cabịt directories & add your ignored patterns lớn the danh sách. If you want lớn indeed watch a directory lượt thích node_modules, you need lớn override the underlying default ignore rules.

Application isn"t restarting

In some networked environments (such as a container running nodetháng reading across a mounted drive), you will need lớn use the legacyWatch: true which enables Chokidar"s polling.

Via the CLI, use either --legacy-watch or -L for short:


nodemon -L
Though this should be a last resort as it will poll every file it can find.

Delaying restarting

In some situations, you may want lớn wait until a number of files have sầu changed. The timeout before checking for new file changes is 1 second. If you"re uploading a number of files & it"s taking some number of seconds, this could cause your ứng dụng to lớn restart multiple times unnecessarily.

To add an extra throttle, or delay restarting, use the --delay command:


nodemon --delay 10 hệ thống.js
For more precision, milliseconds can be specified. Either as a float:


nodemon --delay 2.5 server.js
Or using the time specifier (ms):


nodemon --delay 2500ms hệ thống.js
The delay figure is number of seconds (or milliseconds, if specified) to lớn delay before restarting. So nodetháng will only restart your tiện ích the given number of seconds after the last tệp tin change.

If you are setting this value in nodemon.json, the value will always be interpreted in milliseconds. E.g., the following are equivalent:


nodetháng --delay 2.5 "delay": 2500

Gracefully reloading down your script

It is possible to lớn have sầu nodetháng send any signal that you specify to your application.


nodetháng --signal SIGHUP. hệ thống.js
Your application can handle the signal as follows.


process.once("SIGHUP", function () reloadSomeConfiguration();)
Please note that nodemon will sover this signal lớn every process in the process tree.

If you are using cluster, then each workers (as well as the master) will receive the signal. If you wish lớn terminate all workers on receiving a SIGHUP, a common pattern is lớn catch the SIGHUP in the master, và forward SIGTERM to all workers, while ensuring that all workers ignore SIGHUP.


if (cluster.isMaster) process.on("SIGHUP", function () for (const worker of Object.values(cluster.workers)) worker.process.kill("SIGTERM"); ); else process.on("SIGHUP", function() )

Controlling shutdown of your script

nodetháng sends a kill signal to your application when it sees a tệp tin update. If you need khổng lồ clean up on shutdown inside your script you can capture the kill signal và handle it yourself.

The following example will listen once for the SIGUSR2 signal (used by nodetháng khổng lồ restart), run the clean up process & then kill itself for nodemon khổng lồ continue control:


process.once("SIGUSR2", function () gracefulShutdown(function () process.kill(process.pid, "SIGUSR2"); ););
chú ý that the process.kill is only called once your shutdown jobs are complete. Hat tip khổng lồ Benjie Gillam for writing this technique up.

Triggering events when nodemon state changes

If you want growl like notifications when nodemon restarts or khổng lồ trigger an action when an event happens, then you can either require nodetháng or add event actions lớn your nodetháng.json file.

For example, khổng lồ trigger a notification on a Mac when nodemon restarts, nodetháng.json looks like this:


"events": "restart": "osascript -e "display notification "phầm mềm restarted" with title "nodemon"""
A full list of available events is listed on the event states wiki. chú ý that you can bind khổng lồ both states và messages.

Pipe output lớn somewhere else


Using nodetháng in your gulp workflow

Cheông xã out the gulp-nodemon plugin khổng lồ integrate nodetháng with the rest of your project"s gulp workflow.

Using nodetháng in your Grunt workflow

Cheông chồng out the grunt-nodetháng plugin to lớn integrate nodetháng with the rest of your project"s grunt workflow.

Pronunciation

nodemon, is it pronounced: node-mon, no-demon or node-e-mon (lượt thích pokémon)?

Well...I"ve been asked this many times before. I lượt thích that I"ve sầu been asked this before. There"s been bets as to lớn which one it actually is.

The answer is simple, but possibly frustrating. I"m not saying (how I pronounce it). It"s up khổng lồ you to Điện thoại tư vấn it as you lượt thích. All answers are correct :)

Design principles

Fewer flags is betterWorks across all platformsFewer featuresLet individuals build on top of nodemonOffer all CLI functionality as an APIContributions must have & pass tests

Nodemon is not perfect, and CLI arguments has sprawled beyond where I"m completely happy, but perhaps it can be reduced a little one day.

Xem thêm: Wowy Tên Thật Của Dàn Huấn Luyện Viên Rap Việt, Tiểu Sử Ca Sĩ Wowy

FAQ

See the FAQ and please add your own questions if you think they would help others.

Backers

Thank you khổng lồ all our backers!
Chuyên mục: Ý NGHĨA

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *