[Go] Nodemon basic configuration and use

Source: Internet
Author: User
Tags xquery



In the development environment, often need a tool to automatically restart Project engineering, prior to contact with Python supervisor, now write node found Supervisior in many places have his figure, node also has a NPM module Supervisior is also used To monitor the process, but in addition to Supervisior, there are many other tools, from the GitHub score, the more popular have Forever,nodemon,node-dev, the specific tools of the difference can refer to this article Comparison: Tools to automate restarting node. js Server after Code changes, personally think in the development environment or with Nodemon, because the configuration is more convenient, the document is also very clear. So here we mainly talk about Nodemon first.



Installation of Nodemon:



 
npm install -g nodemon 

After installing Nodemon, you can use Nodemon instead of node to start the application:



nodemon [your node app] (equivalent to node [your node app])


If you do not specify a port in your app, you can specify it in the command:



 
nodemon ./server.js localhost 8080 


You can run debug mode:



 
nodemon --debug ./server.js 80 


To see help, there are a lot of options that are at a glance:



 
nodemon -h or nodemon -help 


Nodemon is one of the reasons for the more popular is that it is more configurable, the following is an example of the configuration file Nodemon.json given by the official website, plus some useful configurations that I have learned, the development environment recommends that each parameter be written on standby, the production environment will not be necessary to remove the parameters, Some fields can be given as parameters in command-line mode and can be viewed by-H, explained below:


  
 
 1 {
 2     "restartable": "rs",
 3     "ignore": [
 4         ".git",
 5         "node_modules/**/node_modules"
 6     ],
 7     "verbose": true,
 8     "execMap": {
 9         "": "node"
10         "js": "node --harmony"
11     },
12     "events": {
13         "restart": "osascript -e ‘display notification \"App restarted due to:\n‘$FILENAME‘\" with title \"nodemon\"‘"
14     },
15     "watch": [
16         "test/fixtures/",
17         "test/samples/"
18     ],
19     "env": {
20         "NODE_ENV": "development",
21         "PORT": "3000"
22     },
23     "ext": "js json",
24     "legacy-watch": false
25 }

<span><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:1.5; " ><strong>restartable</strong>: Reboot command, default is RS, can be changed to your own favorite string. When you start the app with Nodemon, you can directly type RS to restart the service. In addition to the string value, you can also set a value of false, which means that when Nodemon affects your own terminal commands, setting to False does not listen for the RS restart command during Nodemon run. </span></span>



Ignore: Ignore the file suffix name or folder, the file path is written relative to the location of Nodemon.json, the same as the relative path. Nodemon default ignores some files, default ignored files are:. Git, Node_modules, bower_components,. Sass-cache, if these files want to join the monitoring, you need to override the default ignore parameter field Ignoreroot, For example, join: "Ignoreroot": [". Git", "bower_components", ". Sass-cache"], and then add the Node_modules file path to monitor in watch, then Node_modules Also included in the monitoring of the files.



The verbose:true represents the output verbose startup and restart information, such as:






False indicates that these run information is not output, such as:






Execmap: Run the service suffix name and the corresponding Run command, "JS": "Node--harmony" means to use Nodemon instead of node--harmony to run the JS suffix file; "" means www these files without suffix; default defaults The. JS configuration file identifies some files: py: ' Python ', RB: ' Ruby '.



Events: This field represents some of the triggering events when Nodemon is running to some states, with a total of five states:


    • Start-sub-process (that is, the monitored application) starts

    • Crash-child process crashes, exit is not triggered

    • Exit-the child process exits completely, not a non-normal crash

    • Restart-Child process restart

    • Config:update-nodemon config file changed


The status can be followed by standard input and output statements, such as the Mac system settings: "Start": "Echo ' app start", the app will be launched when the start message, and other similar commands such as LS,PS and other standard commands can be defined here. In addition, can also write JS to monitor, GitHub on the introduction: EVENTS.MD, but I tried after not success, if you have a friend, remember in the comments to enlighten. ^_^



Watch: The folder path or file path of the monitor.



ENV: Operating environment development is a development environment, production is a production environment. Port is the port number.



Ext: Monitors the file with the specified suffix, with a space interval. Default monitoring suffix file:. js,. Coffee,. Litcoffee,. Json. But for files without file suffix, such as www file, I can not find how to use Nodemon to monitor, even if included in watch, Nodemon will be ignored.



Note: In order to monitor and ignore the file changes in a sequence of problems, or priority, first Nodemon will read the watch inside the file or file path to be monitored, and then select from the file to monitor the suffix specified in ext, and finally remove the ignored file or file path specified from the Ignore.



Legacy-watch:nodemon uses Chokidar as the underlying monitoring system, but if the monitoring fails, or if there are no files to monitor, you need to use the polling mode (polling modes), which is set Legacy-watch to True, You can also specify on the command line:



$ nodemon --legacy-watch
$ nodemon -L # shorthand 


<span><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:1.5; " > Paste out the default configuration file for Nodemon &nbsp;</span><a style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:1.5; "href=" Https://github.com/remy/nodemon/blob/master/lib/config/defaults.js ">default.js</a ><span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:1.5; " >:</span></span>



 1 // default options for config.options
 2 module.exports = {
 3     restartable: ‘rs‘,
 4     colours: true,
 5     execMap: {
 6         py: ‘python‘,
 7         rb: ‘ruby‘,
 8         // more can be added here such as ls: lsc - but please ensure it‘s cross
 9         // compatible with linux, mac and windows, or make the default.js
10         // dynamically append the `.cmd` for node based utilities
11     },
12     ignoreRoot: [‘.git‘, ‘node_modules‘, ‘bower_components‘, ‘.sass-cache‘],
13     watch: [‘*.*‘],
14     stdin: true,
15     runOnChangeOnly: false,
16     verbose: false,
17     // ‘stdout‘ refers to the default behaviour of a required nodemon‘s child,
18     // but also includes stderr. If this is false, data is still dispatched via
19     // nodemon.on(‘stdout/stderr‘)
20     stdout: true,
21 };


There are several less-used configuration fields:



1) colous : Output information color indication.



2) runonchangeonly : True when running the Nodemon www project will not start, only to maintain the monitoring of the file, when the monitoring of the file has been modified and saved to start the application, the other has no effect. The default is false to start the app and monitor file changes from the start.



3) Stdin,stdout : This is about the standard input and output settings, the above mentioned in the Nodemon.json file in the Events field can set the standard input and output statements, if set to false, the standard input input statement will be effective.



GitHub gives a faq.js answer to some common questions, some of which have already been mentioned, and some of the more common ones are listed below:



1) When your application starts the service band parameters and Nodemon conflicts, you can use the following method to resolve the conflict:



 
$ nodemon app.js -- -L -opt2 -opt3 


By----to separate, Nodemon not to read--the parameters behind, but passed to App.js.



2) When the application crashes for some reason, the Nodemon does not restart automatically, and the following information is output:



 
[nodemon] app crashed - waiting for file changes before starting... 


This time need to modify the file and save Nodemon will not restart the application, this does not matter in the development environment, but if you want to put Nodemon online, we often want Nodemon to automatically restart the crash application, this time need forever to assist, there is a issue Specifically speaking about this problem. When you use forever to restart Nodemon, you need to add a parameter--exitcrash when Nodemon starts:



 
nodemon www --exitcrash 


When the application crashes, Nodemon automatically interrupts the exit, forever detects that the Nodemon restarts Nodemon,nodemon and restarts the app. The other is forever configuration, because here only talk about Nodemon, so do not involve forever, then summarize forever time again, interested to see here.



3) If you want to start the app with the NPM start command and want to use Nodemon to monitor file changes, you can modify the Scripts.start in NPM's Package.js file:



 
 
1 "scripts": {
2     "start": "nodemon ./bin/www"
3 }


Then start with NPM start is execute nodemon./bin/www.



Reference Documentation:



GitHub Remy/nodemon. Readme.md



GitHub Remy/nodemon. Faq.md



GitHub Remy/nodemon. Events.md



CodePlex. Nodemon



Bubkoo. Using Nodemon in Express development



Qihan Zhang. Comparison:tools to automate restarting node. js Server after Code changes



The level is limited, wrong to welcome correct. Original blog, reproduced please indicate the source.



[Go] Nodemon basic configuration and use


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.