UNPKG

13.6 kBMarkdownView Raw
1![nodemon logo](http://nodemon.io/nodemon.svg)
2
3# nodemon
4
5For use during development of a node.js based application.
6
7nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
8
9nodemon does **not** require *any* changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for `node`, think of it as replacing the word "node" on the command line when you run your script.
10
11[![NPM version](https://badge.fury.io/js/nodemon.svg)](https://npmjs.org/package/nodemon)
12[![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)
13
14# Installation
15
16Either through cloning with git or by using [npm](http://npmjs.org) (the recommended way):
17
18```
19npm install -g nodemon
20```
21
22And nodemon will be installed globally to your system path.
23
24You can also install nodemon as a developement dependency:
25
26```
27npm install --save-dev nodemon
28```
29
30With 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`.
31
32# Usage
33
34nodemon wraps your application, so you can pass all the arguments you would normally pass to your app:
35
36```
37nodemon [your node app]
38```
39
40For CLI options, use the `-h` (or `--help`) argument:
41
42```
43nodemon -h
44```
45
46Using nodemon is simple, if my application accepted a host and port as the arguments, I would start it as so:
47
48```
49nodemon ./server.js localhost 8080
50```
51
52Any output from this script is prefixed with `[nodemon]`, otherwise all output from your application, errors included, will be echoed out as expected.
53
54If 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)).
55
56You can also pass the `inspect` flag to node through the command line as you would normally:
57
58```
59nodemon --inspect ./server.js 80
60```
61
62If 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.
63
64nodemon will also search for the `scripts.start` property in `package.json` (as of nodemon 1.1.x).
65
66Also check out the [FAQ](https://github.com/remy/nodemon/blob/master/faq.md) or [issues](https://github.com/remy/nodemon/issues) for nodemon.
67
68## Automatic re-running
69
70nodemon 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.
71
72## Manual restarting
73
74Whilst nodemon is running, if you need to manually restart your application, instead of stopping and restart nodemon, you can simply type `rs` with a carriage return, and nodemon will restart your process.
75
76## Config files
77
78nodemon 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.
79
80The specificity is as follows, so that a command line argument will always override the config file settings:
81
82- command line arguments
83- local config
84- global config
85
86A config file can take any of the command line arguments as JSON key values, for example:
87
88```json
89{
90 "verbose": true,
91 "ignore": ["*.test.js", "fixtures/*"],
92 "execMap": {
93 "rb": "ruby",
94 "pde": "processing --sketch={{pwd}} --run"
95 }
96}
97```
98
99The above `nodemon.json` file might be my global config so that I have support for ruby files and processing files, and I can simply run `nodemon demo.pde` and nodemon will automatically know how to run the script even though out of the box support for processing scripts.
100
101A further example of options can be seen in [sample-nodemon.md](https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
102
103### package.json
104
105If you want to keep all your package configurations in one place, nodemon supports using `package.json` for configuration.
106Simply 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`:
107
108```json
109{
110 "name": "nodemon",
111 "homepage": "http://nodemon.io",
112 "...": "... other standard package.json values",
113 "nodemonConfig": {
114 "ignore": ["test/*", "docs/*"],
115 "delay": "2500"
116 }
117}
118```
119
120Note that if you specify a `--config` file or provide a local `nodemon.json` any `package.json` config is ignored.
121
122*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))*.
123
124## Using nodemon as a module
125
126Please see [doc/requireable.md](doc/requireable.md)
127
128## Running non-node scripts
129
130nodemon 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`:
131
132```
133nodemon --exec "python -v" ./app.py
134```
135
136Now 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.
137
138### Default executables
139
140Using 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.
141
142To add support for nodemon to know about the .pl extension (for Perl), the nodemon.json file would add:
143
144```json
145{
146 "execMap": {
147 "pl": "perl"
148 }
149}
150```
151
152Now running the following, nodemon will know to use `perl` as the executable:
153
154```
155nodemon script.pl
156```
157
158It'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.
159
160## Monitoring multiple directories
161
162By default nodemon monitors the current working directory. If you want to take control of that option, use the `--watch` option to add specific paths:
163
164```
165nodemon --watch app --watch libs app/server.js
166```
167
168Now 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.
169
170Don't use unix globbing to pass multiple directories, e.g `--watch ./lib/*`, it won't work. You need a `--watch` flag per directory watched.
171
172## Specifying extension watch list
173
174By 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:
175
176```
177nodemon -e js,jade
178```
179
180Now nodemon will restart on any changes to files in the directory (or subdirectories) with the extensions .js, .jade.
181
182## Ignoring files
183
184By 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.
185
186This can be done via the command line:
187
188```
189nodemon --ignore lib/ --ignore tests/
190```
191
192Or specific files can be ignored:
193
194```
195nodemon --ignore lib/app.js
196```
197
198Patterns can also be ignored (but be sure to quote the arguments):
199
200```
201nodemon --ignore 'lib/*.js'
202```
203
204Note 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).
205
206## Application isn't restarting
207
208In 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.
209
210Via the CLI, use either `--legacy-watch` or `-L` for short:
211
212```
213nodemon -L
214```
215
216Though this should be a last resort as it will poll every file it can find.
217
218## Delaying restarting
219
220In 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.
221
222To add an extra throttle, or delay restarting, use the `--delay` command:
223
224```
225nodemon --delay 10 server.js
226```
227
228For more precision, milliseconds can be specified. Either as a float:
229
230```
231nodemon --delay 2.5 server.js
232```
233
234Or using the time specifier (ms):
235
236```
237nodemon --delay 2500ms server.js
238```
239
240The 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.
241
242If you are setting this value in `nodemon.json`, the value will always be interpretted in milliseconds. E.g., the following are equivalent:
243
244```
245nodemon --delay 2.5
246
247{
248 "delay": "2500"
249}
250```
251
252## Controlling shutdown of your script
253
254nodemon 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.
255
256The 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:
257
258```js
259process.once('SIGUSR2', function () {
260 gracefulShutdown(function () {
261 process.kill(process.pid, 'SIGUSR2');
262 });
263});
264```
265
266Note 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.
267
268## Triggering events when nodemon state changes
269
270If you want growl like notifications when nodemon restarts or to trigger an action when an event happens, then you can either `require` nodemon or simply add event actions to your `nodemon.json` file.
271
272For example, to trigger a notification on a Mac when nodemon restarts, `nodemon.json` looks like this:
273
274```json
275{
276 "events": {
277 "restart": "osascript -e 'display notification \"app restarted\" with title \"nodemon\"'"
278 }
279}
280```
281
282A 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.
283
284## Pipe output to somewhere else
285
286```js
287nodemon({
288 script: ...,
289 stdout: false // important: this tells nodemon not to output to console
290}).on('readable', function() { // the `readable` event indicates that data is ready to pick up
291 this.stdout.pipe(fs.createWriteStream('output.txt'));
292 this.stderr.pipe(fs.createWriteStream('err.txt'));
293});
294```
295
296## Using nodemon in your gulp workflow
297
298Check out the [gulp-nodemon](https://github.com/JacksonGariety/gulp-nodemon) plugin to integrate nodemon with the rest of your project's gulp workflow.
299
300## Using nodemon in your Grunt workflow
301
302Check out the [grunt-nodemon](https://github.com/ChrisWren/grunt-nodemon) plugin to integrate nodemon with the rest of your project's grunt workflow.
303
304## Pronunciation
305
306> nodemon, is it pronounced: node-mon, no-demon or node-e-mon (like pokémon)?
307
308Well...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.
309
310The 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 :)
311
312## Design principles
313
314- Less flags is better
315- Works across all platforms
316- Less features
317- Let individuals build on top of nodemon
318- Offer all CLI functionality as an API
319- Contributions must have and pass tests
320
321Nodemon is not perfect, and CLI arguments has sprawled beyond where I'm completely happy, but perhaps it can be reduced a little one day.
322
323## FAQ
324
325See the [FAQ](https://github.com/remy/nodemon/blob/master/faq.md) and please add your own questions if you think they would help others.
326
327## Backers
328
329Thank you to all [our backers](https://opencollective.com/nodemon#backer)! 🙏
330
331[![nodemon backers](https://opencollective.com/nodemon/backers.svg?width=890)](https://opencollective.com/nodemon#backers)
332
333## Sponsors
334
335Support 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)
336
337[<img src="website/sparkpost.svg" width="200">](https://sparkpo.st/nodemon)
338
339# License
340
341MIT [http://rem.mit-license.org](http://rem.mit-license.org)