Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

README.md 15KB

před 5 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <p align="center">
  2. <img src="https://user-images.githubusercontent.com/13700/35731649-652807e8-080e-11e8-88fd-1b2f6d553b2d.png" alt="Nodemon Logo">
  3. </p>
  4. # nodemon
  5. nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
  6. nodemon does **not** require *any* additional changes to your code or method of development. nodemon is a replacement wrapper for `node`. To use `nodemon`, replace the word `node` on the command line when executing your script.
  7. [![NPM version](https://badge.fury.io/js/nodemon.svg)](https://npmjs.org/package/nodemon)
  8. [![Travis Status](https://travis-ci.org/remy/nodemon.svg?branch=master)](https://travis-ci.org/remy/nodemon) [![Backers on Open Collective](https://opencollective.com/nodemon/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/nodemon/sponsors/badge.svg)](#sponsors)
  9. # Installation
  10. Either through cloning with git or by using [npm](http://npmjs.org) (the recommended way):
  11. ```bash
  12. npm install -g nodemon
  13. ```
  14. And nodemon will be installed globally to your system path.
  15. You can also install nodemon as a development dependency:
  16. ```bash
  17. npm install --save-dev nodemon
  18. ```
  19. With a local installation, nodemon will not be available in your system path. Instead, the local installation of nodemon can be run by calling it from within an npm script (such as `npm start`) or using `npx nodemon`.
  20. # Usage
  21. nodemon wraps your application, so you can pass all the arguments you would normally pass to your app:
  22. ```bash
  23. nodemon [your node app]
  24. ```
  25. For CLI options, use the `-h` (or `--help`) argument:
  26. ```bash
  27. nodemon -h
  28. ```
  29. Using nodemon is simple, if my application accepted a host and port as the arguments, I would start it as so:
  30. ```bash
  31. nodemon ./server.js localhost 8080
  32. ```
  33. Any output from this script is prefixed with `[nodemon]`, otherwise all output from your application, errors included, will be echoed out as expected.
  34. If no script is given, nodemon will test for a `package.json` file and if found, will run the file associated with the *main* property ([ref](https://github.com/remy/nodemon/issues/14)).
  35. You can also pass the `inspect` flag to node through the command line as you would normally:
  36. ```bash
  37. nodemon --inspect ./server.js 80
  38. ```
  39. If you have a `package.json` file for your app, you can omit the main script entirely and nodemon will read the `package.json` for the `main` property and use that value as the app.
  40. nodemon will also search for the `scripts.start` property in `package.json` (as of nodemon 1.1.x).
  41. Also check out the [FAQ](https://github.com/remy/nodemon/blob/master/faq.md) or [issues](https://github.com/remy/nodemon/issues) for nodemon.
  42. ## Automatic re-running
  43. nodemon was originally written to restart hanging processes such as web servers, but now supports apps that cleanly exit. If your script exits cleanly, nodemon will continue to monitor the directory (or directories) and restart the script if there are any changes.
  44. ## Manual restarting
  45. Whilst nodemon is running, if you need to manually restart your application, instead of stopping and restart nodemon, you can type `rs` with a carriage return, and nodemon will restart your process.
  46. ## Config files
  47. nodemon supports local and global configuration files. These are usually named `nodemon.json` and can be located in the current working directory or in your home directory. An alternative local configuration file can be specified with the `--config <file>` option.
  48. The specificity is as follows, so that a command line argument will always override the config file settings:
  49. - command line arguments
  50. - local config
  51. - global config
  52. A config file can take any of the command line arguments as JSON key values, for example:
  53. ```json
  54. {
  55. "verbose": true,
  56. "ignore": ["*.test.js", "fixtures/*"],
  57. "execMap": {
  58. "rb": "ruby",
  59. "pde": "processing --sketch={{pwd}} --run"
  60. }
  61. }
  62. ```
  63. The above `nodemon.json` file might be my global config so that I have support for ruby files and processing files, and I can run `nodemon demo.pde` and nodemon will automatically know how to run the script even though out of the box support for processing scripts.
  64. A further example of options can be seen in [sample-nodemon.md](https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
  65. ### package.json
  66. If you want to keep all your package configurations in one place, nodemon supports using `package.json` for configuration.
  67. 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`:
  68. ```json
  69. {
  70. "name": "nodemon",
  71. "homepage": "http://nodemon.io",
  72. "...": "... other standard package.json values",
  73. "nodemonConfig": {
  74. "ignore": ["test/*", "docs/*"],
  75. "delay": "2500"
  76. }
  77. }
  78. ```
  79. Note that if you specify a `--config` file or provide a local `nodemon.json` any `package.json` config is ignored.
  80. *This section needs better documentation, but for now you can also see `nodemon --help config` ([also here](https://github.com/remy/nodemon/blob/master/doc/cli/config.txt))*.
  81. ## Using nodemon as a module
  82. Please see [doc/requireable.md](doc/requireable.md)
  83. ## Using nodemon as child process
  84. Please see [doc/events.md](doc/events.md#Using_nodemon_as_child_process)
  85. ## Running non-node scripts
  86. nodemon can also be used to execute and monitor other programs. nodemon will read the file extension of the script being run and monitor that extension instead of `.js` if there's no `nodemon.json`:
  87. ```bash
  88. nodemon --exec "python -v" ./app.py
  89. ```
  90. Now nodemon will run `app.py` with python in verbose mode (note that if you're not passing args to the exec program, you don't need the quotes), and look for new or modified files with the `.py` extension.
  91. ### Default executables
  92. Using the `nodemon.json` config file, you can define your own default executables using the `execMap` property. This is particularly useful if you're working with a language that isn't supported by default by nodemon.
  93. To add support for nodemon to know about the `.pl` extension (for Perl), the `nodemon.json` file would add:
  94. ```json
  95. {
  96. "execMap": {
  97. "pl": "perl"
  98. }
  99. }
  100. ```
  101. Now running the following, nodemon will know to use `perl` as the executable:
  102. ```bash
  103. nodemon script.pl
  104. ```
  105. It's generally recommended to use the global `nodemon.json` to add your own `execMap` options. However, if there's a common default that's missing, this can be merged in to the project so that nodemon supports it by default, by changing [default.js](https://github.com/remy/nodemon/blob/master/lib/config/defaults.js) and sending a pull request.
  106. ## Monitoring multiple directories
  107. By default nodemon monitors the current working directory. If you want to take control of that option, use the `--watch` option to add specific paths:
  108. ```bash
  109. nodemon --watch app --watch libs app/server.js
  110. ```
  111. Now nodemon will only restart if there are changes in the `./app` or `./libs` directory. By default nodemon will traverse sub-directories, so there's no need in explicitly including sub-directories.
  112. Don't use unix globbing to pass multiple directories, e.g `--watch ./lib/*`, it won't work. You need a `--watch` flag per directory watched.
  113. ## Specifying extension watch list
  114. By default, 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 list with the `-e` (or `--ext`) switch like so:
  115. ```bash
  116. nodemon -e js,pug
  117. ```
  118. Now nodemon will restart on any changes to files in the directory (or subdirectories) with the extensions `.js`, `.pug`.
  119. ## Ignoring files
  120. By default, nodemon will only restart when a `.js` JavaScript file changes. In some cases you will want to ignore some specific files, directories or file patterns, to prevent nodemon from prematurely restarting your application.
  121. This can be done via the command line:
  122. ```bash
  123. nodemon --ignore lib/ --ignore tests/
  124. ```
  125. Or specific files can be ignored:
  126. ```bash
  127. nodemon --ignore lib/app.js
  128. ```
  129. Patterns can also be ignored (but be sure to quote the arguments):
  130. ```bash
  131. nodemon --ignore 'lib/*.js'
  132. ```
  133. Note that by default, nodemon will ignore the `.git`, `node_modules`, `bower_components`, `.nyc_output`, `coverage` and `.sass-cache` directories and *add* your ignored patterns to the list. If you want to indeed watch a directory like `node_modules`, you need to [override the underlying default ignore rules](https://github.com/remy/nodemon/blob/master/faq.md#overriding-the-underlying-default-ignore-rules).
  134. ## Application isn't restarting
  135. In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the `legacyWatch: true` which enables Chokidar's polling.
  136. Via the CLI, use either `--legacy-watch` or `-L` for short:
  137. ```bash
  138. nodemon -L
  139. ```
  140. Though this should be a last resort as it will poll every file it can find.
  141. ## Delaying restarting
  142. In some situations, you may want to wait until a number of files have changed. The timeout before checking for new file changes is 1 second. If you're uploading a number of files and it's taking some number of seconds, this could cause your app to restart multiple times unnecessarily.
  143. To add an extra throttle, or delay restarting, use the `--delay` command:
  144. ```bash
  145. nodemon --delay 10 server.js
  146. ```
  147. For more precision, milliseconds can be specified. Either as a float:
  148. ```bash
  149. nodemon --delay 2.5 server.js
  150. ```
  151. Or using the time specifier (ms):
  152. ```bash
  153. nodemon --delay 2500ms server.js
  154. ```
  155. The delay figure is number of seconds (or milliseconds, if specified) to delay before restarting. So nodemon will only restart your app the given number of seconds after the *last* file change.
  156. If you are setting this value in `nodemon.json`, the value will always be interpreted in milliseconds. E.g., the following are equivalent:
  157. ```bash
  158. nodemon --delay 2.5
  159. {
  160. "delay": "2500"
  161. }
  162. ```
  163. ## Gracefully reloading down your script
  164. It is possible to have nodemon send any signal that you specify to your application.
  165. ```bash
  166. nodemon --signal SIGHUP server.js
  167. ```
  168. Your application can handle the signal as follows.
  169. ```js
  170. process.once("SIGHUP", function () {
  171. reloadSomeConfiguration();
  172. })
  173. ```
  174. Please note that nodemon will send this signal to every process in the process tree.
  175. If you are using `cluster`, then each workers (as well as the master) will receive the signal. If you wish to terminate all workers on receiving a `SIGHUP`, a common pattern is to catch the `SIGHUP` in the master, and forward `SIGTERM` to all workers, while ensuring that all workers ignore `SIGHUP`.
  176. ```js
  177. if (cluster.isMaster) {
  178. process.on("SIGHUP", function () {
  179. for (const worker of Object.values(cluster.workers)) {
  180. worker.process.kill("SIGTERM");
  181. }
  182. });
  183. } else {
  184. process.on("SIGHUP", function() {})
  185. }
  186. ```
  187. ## Controlling shutdown of your script
  188. nodemon sends a kill signal to your application when it sees a file update. If you need to clean up on shutdown inside your script you can capture the kill signal and handle it yourself.
  189. The following example will listen once for the `SIGUSR2` signal (used by nodemon to restart), run the clean up process and then kill itself for nodemon to continue control:
  190. ```js
  191. process.once('SIGUSR2', function () {
  192. gracefulShutdown(function () {
  193. process.kill(process.pid, 'SIGUSR2');
  194. });
  195. });
  196. ```
  197. Note that the `process.kill` is *only* called once your shutdown jobs are complete. Hat tip to [Benjie Gillam](http://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/) for writing this technique up.
  198. ## Triggering events when nodemon state changes
  199. If you want growl like notifications when nodemon restarts or to trigger an action when an event happens, then you can either `require` nodemon or add event actions to your `nodemon.json` file.
  200. For example, to trigger a notification on a Mac when nodemon restarts, `nodemon.json` looks like this:
  201. ```json
  202. {
  203. "events": {
  204. "restart": "osascript -e 'display notification \"app restarted\" with title \"nodemon\"'"
  205. }
  206. }
  207. ```
  208. A full list of available events is listed on the [event states wiki](https://github.com/remy/nodemon/wiki/Events#states). Note that you can bind to both states and messages.
  209. ## Pipe output to somewhere else
  210. ```js
  211. nodemon({
  212. script: ...,
  213. stdout: false // important: this tells nodemon not to output to console
  214. }).on('readable', function() { // the `readable` event indicates that data is ready to pick up
  215. this.stdout.pipe(fs.createWriteStream('output.txt'));
  216. this.stderr.pipe(fs.createWriteStream('err.txt'));
  217. });
  218. ```
  219. ## Using nodemon in your gulp workflow
  220. Check out the [gulp-nodemon](https://github.com/JacksonGariety/gulp-nodemon) plugin to integrate nodemon with the rest of your project's gulp workflow.
  221. ## Using nodemon in your Grunt workflow
  222. Check out the [grunt-nodemon](https://github.com/ChrisWren/grunt-nodemon) plugin to integrate nodemon with the rest of your project's grunt workflow.
  223. ## Pronunciation
  224. > nodemon, is it pronounced: node-mon, no-demon or node-e-mon (like pokémon)?
  225. Well...I've been asked this many times before. I like that I've been asked this before. There's been bets as to which one it actually is.
  226. The answer is simple, but possibly frustrating. I'm not saying (how I pronounce it). It's up to you to call it as you like. All answers are correct :)
  227. ## Design principles
  228. - Fewer flags is better
  229. - Works across all platforms
  230. - Fewer features
  231. - Let individuals build on top of nodemon
  232. - Offer all CLI functionality as an API
  233. - Contributions must have and pass tests
  234. 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.
  235. ## FAQ
  236. See the [FAQ](https://github.com/remy/nodemon/blob/master/faq.md) and please add your own questions if you think they would help others.
  237. ## Backers
  238. Thank you to all [our backers](https://opencollective.com/nodemon#backer)! 🙏
  239. [![nodemon backers](https://opencollective.com/nodemon/backers.svg?width=890)](https://opencollective.com/nodemon#backers)
  240. ## Sponsors
  241. Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Sponsor this project today ❤️](https://opencollective.com/nodemon#sponsor)
  242. [<img src="https://user-images.githubusercontent.com/13700/35731651-677aa3fc-080e-11e8-8580-75fa92db56ec.png" width="200">](https://sparkpo.st/nodemon)
  243. [<img src="https://user-images.githubusercontent.com/13700/35731622-421d4466-080e-11e8-8ddc-11c70e1cd79e.png" width="200">](https://mixmax.com)
  244. # License
  245. MIT [http://rem.mit-license.org](http://rem.mit-license.org)