[英]How to use nodemon in npm scripts to build and start scripts?
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon lib/index.js --exec npm run build"
}
使用命令npm run watch
導致npm run watch
以下錯誤命令: [nodemon] starting "npm lib/index.js run build"
我將如何編寫一個 nodemon 命令,在重新加載時,使用 babel 轉換代碼並重新加載代碼?
您可以有兩個 nodemon,一個用於轉譯,另一個用於運行您的代碼。 在 package.json 你可以做這樣的事情:
"scripts": {
"serve": "nodemon --watch dist/ ./dist/index.js",
"build" : "nodemon --watch src/ --exec babel ./src --out-dir ./dist --source-maps --copy-files"
},
更好的選擇是不使用全局安裝,而是使用本地安裝的包。 這也有助於自動化構建,這些構建可能與每個 12 因子應用程序設計的本地機器設置相同。
"scripts": {
"watch": "node ./node_modules/nodemon/bin/nodemon.js"
}
"scripts": {
"build": "babel src -d lib",
"start": "nodemon --exec babel-node lib/index.js",
"serve": "npm run build && node lib/index.js"
}
Serve 用於生產,使用 npm start 你要做的是先轉譯,然后運行 nodemon。
有一個選項可以在“監視”模式下使用 Babel 構建文件,讓 Nodemon 僅監視“構建”文件夾,並在編譯輸出發生更改時重新啟動應用程序。
{
"name": "app",
"version": "1.0.0",
"private": true,
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"nodemon": "^1.19.2"
},
"scripts": {
"build": "babel src --out-dir build --source-maps=inline --verbose",
"start": "yarn build --watch & sleep 1 && nodemon --watch build build/index.js"
}
}
此示例取自 GitHub 上的GraphQL API Examples
存儲庫。
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon --exec \"npm run build && node lib/index.js\" -e js --ignore lib/"
}
然后運行npm run watch
。 此后,每次修改源代碼( .js
文件)時,nodemon 都會重建項目,然后重新啟動服務器。
--exec
指定您希望 nodemon 在文件更改時執行哪些非節點腳本(也適用於節點node lib/index.js
上面的節點腳本)。
-e
指定您希望 nodemon 監視的文件擴展名。
--ignore
指定您希望 nodemon 忽略的文件/目錄。 此選項對於解決此問題至關重要,因為如果您不指定忽略此lib/
文件夾,則 nodemon 將無限重啟,因為lib/
中的編譯文件也是.js
文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.