[轉] nodemon 基本配置與使用

來源:互聯網
上載者:User

標籤:height   onchange   連接埠號碼   應用   stderr   rri   under   路徑   參數   

在開發環境下,往往需要一個工具來自動重啟項目工程,之前接觸過 python 的 supervisor,現在寫 node 的時候發現 supervisior 在很多地方都有他的身影,node 也有一個 npm 模組 supervisior 也是用來監控進程的,不過除了 supervisior 外,還有很多其他的工具,從 github 的評分上看,比較熱門的有 forever,nodemon,node-dev,具體這些工具的區別可以參考這篇文章  Comparison: Tools to Automate Restarting Node.js Server After Code Changes ,個人覺得在開發環境還是用 nodemon,因為配置比較方便,文檔也很清晰。所以這裡先主要講 nodemon。

nodemon 的安裝:

npm install -g nodemon

安裝完 nodemon 後,就可以用 nodemon 來代替 node 來啟動應用:

nodemon [your node app](相當於 node [your node app])

如果沒有在應用中指定連接埠,可以在命令中指定:

nodemon ./server.js localhost 8080

可以運行 debug 模式:

nodemon --debug ./server.js 80

查看協助,協助裡面有很多選項都是一目瞭然:

nodemon -h 或者 nodemon -help

nodemon 比較流行的原因之一就是它的可配置性比較高,下面是官網給出的設定檔  nodemon.json  的例子,加上我自己瞭解到的有用的一些配置,開發環境建議可以把每個參數都寫上備用,生產環境就把沒有必要的參數去掉,有些欄位是可以在命令列模式以參數形式給出的,可以通過 -h 查看,下面逐個解釋:

 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": false25 }View Code

<span><span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;"><strong>restartable</strong>:重啟的命令,預設是 rs ,可以改成你自己喜歡的字串。當用 nodemon 啟動應用時,可以直接鍵入 rs 直接重啟服務。除了字串值外,還可以設定 false 值,這個值的意思是當 nodemon 影響了你自己的終端命令時,設定為 false 則不會在 nodemon 運行期間監聽 rs 的重啟命令。</span></span>

ignore:忽略的檔案尾碼名或者檔案夾,檔案路徑的書寫用相對於 nodemon.json 所在位置的相對路徑,下同。nodemon 會預設忽略一些檔案,預設忽略的檔案有:.git, node_modules, bower_components, .sass-cache,如果這些檔案想要加入監控,需要重寫預設忽略參數欄位 ignoreRoot,比如加入:"ignoreRoot": [".git", "bower_components", ".sass-cache"],然後在 watch 中將 node_modules 檔案路徑加入監控,那麼 node_modules 內的檔案也加入了監控了。

verbose:true 表示輸出詳細啟動與重啟資訊,如:

false 表示不輸出這些運行資訊,如:

execMap:運行服務的尾碼名和對應的運行命令,"js": "node --harmony" 表示用 nodemon 代替 node  --harmony 運行 js 尾碼檔案;"" 指 www 這些沒有尾碼名的檔案;預設的 defaults.js 設定檔會識別一些檔案:py: ‘python‘,rb: ‘ruby‘。

events:這個欄位表示 nodemon 運行到某些狀態時的一些觸發事件,總共有五個狀態:

  • start - 子進程(即監控的應用)啟動

  • crash - 子進程崩潰,不會觸發 exit

  • exit - 子進程完全退出,不是非正常的崩潰

  • restart - 子進程重啟

  • config:update - nodemon 的 config 檔案改變

狀態後面可以帶標準輸入輸出語句,比如 mac 系統下設定: "start": "echo ‘app start‘",那麼啟動應用時會輸出 app start 資訊,其他類似命令如 ls,ps 等等標準命令都可以在這裡定義。除此之外,也可以寫js來監控,github 上有介紹: events.md ,不過我試過之後沒有成功,如果有懂的朋友,記得在評論不吝賜教。^_^

watch:監控的檔案夾路徑或者檔案路徑。

env:運行環境 development 是開發環境,production 是生產環境。port 是連接埠號碼。

ext:監控指定尾碼名的檔案,用空格間隔。預設監控的尾碼檔案:.js, .coffee, .litcoffee, .json。但是對於沒有檔案尾碼的檔案,比如 www 檔案,我暫時找不到怎麼用 nodemon 去監控,就算在 watch 中包含了,nodemon 也會忽略掉。

註:關於監控以及忽略檔案修改有個順序的問題,或者說優先順序,首先 nodemon 會先讀取 watch 裡面需要監控的檔案或檔案路徑,再從檔案中選擇監控 ext 中指定的尾碼名,最後去掉從 ignore 中指定的忽略檔案或檔案路徑。

legacy-watch:nodemon 使用  Chokidar 作為底層監控系統,但是如果監控失效,或者提示沒有需要監控的檔案時,就需要使用輪詢模式(polling mode),即設定 legacy-watch 為 true,也可以在命令列中指定:

$ nodemon --legacy-watch $ nodemon -L # 簡寫

<span><span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;">下面貼出 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.js10 // dynamically append the `.cmd` for node based utilities11 },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 via19 // nodemon.on(‘stdout/stderr‘)20 stdout: true,21 };View Code

有幾個比較少用到的配置欄位:

1) colous :輸出資訊顏色標示。

2) runOnChangeOnly :true 時運行 nodemon www 項目不會啟動,只保持對檔案的監控,當監控的檔案有修改並儲存時才會啟動應用,其他沒有影響。預設是 false 即一開始就啟動應用並監控檔案改動。

3) stdin,stdout :這個是關於標準輸入輸出的設定,上文提到 nodemon.json 檔案中的 events 欄位可以為狀態設定標準輸入輸出語句,如果這裡設定了 false,標準輸入輸入語句就會實效。

github 上給出了一個  faq.js 解答了一些常見的問題,有的上文已經提到,還有一些比較常見的列舉如下:

1)當自己的應用啟動服務帶的參數和 nodemon 衝突時,可以利用下面的方法來解決衝突:

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

以 -- 為分隔,nodemon 不會去讀取 -- 後面的參數,而是傳給 app.js。

2)當應用因為某些原因奔潰時,nodemon 不會自動重啟,會輸出以下資訊:

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

這個時需要修改檔案並儲存後 nodemon 才會重啟應用,這在開發環境沒什麼關係,但是如果想把 nodemon 放線上上時,我們往往希望 nodemon 能夠自動重啟崩潰的應用,這個時候就需要 forever 來輔助了,有一個  issue 專門講這個問題。使用 forever 來重啟 nodemon 時,在 nodemon 啟動時需要加個參數 --exitcrash:

nodemon www --exitcrash

這樣當應用崩潰後,nodemon 會自動中斷退出,forever 檢測到 nodemon 退出後就會重啟 nodemon,nodemon 又會重啟應用。其他就是 forever 的配置了,因為這裡只講 nodemon,所以就不涉及 forever,到時候總結 forever 的時候再講,感興趣的看 這裡 。

3)如果想通過 npm start 命令來啟動應用同時又想用 nodemon 來監控檔案改動,可以修改 npm 的 package.js 檔案中的 scripts.start:

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

那麼用 npm start 啟動後就是執行 nodemon ./bin/www。

參考文檔:

github remy/nodemon .  README.md

github remy/nodemon . faq.md

github remy/nodemon . events.md

codeplex . nodemon

bubkoo .  在 Express 開發中使用 nodemon

Qihan Zhang .  Comparison: Tools to Automate Restarting Node.js Server After Code Changes

水平有限,錯誤歡迎指正。原創博文,轉載請註明出處。

[轉] nodemon 基本配置與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.