プロジェクトは今日オンラインになっており、node.js の作業を続ける時間を割くことができます。
私は長い間 vue.js に取り組んできましたが、無意識のうちにホットリロードや Ctrl+S による保存に頼っていました。!ページには最新のステータスが自動的に表示されます。!シンプルにかっこいい!!
その結果、node.jsで作業するときに、ctrl+sで保存し、最新のステータスを期待して待って、結果は...え?? おお。。これはnode.jsであり、コマンドを入力する必要があります
再コンパイルして起動できるのは「node [起動ファイル]」だけです(嫌な顔~)
そこで、ある程度waveを探してみたところ、nodemon(自動再起動モジュール)というものを見つけました。
--------------------------------------------------------------
プロセス:
1. インストールの依存関係
グローバルインストール:
npm install -g nodemon // 8月23更正
ローカルインストール:
npm install nodemon --save // 8月23更正
インストールが完了したら、nodemon -v を使用してバージョンを表示し、インストールが成功したかどうかを確認できます。
2. ノードモンを起動します
コマンドラインを入力します:
nodemon
出力:
[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
3. CLI オプションについて
コマンドラインを入力します:
nodemon -h
出力:
Options:
--config file ............ alternate nodemon.json config file to use // 备用nodemon.json配置文件使用
-e, --ext ................ extensions to look for, ie. js,jade,hbs. // 监控指定后缀名的文件
-x, --exec app ........... execute script with "app", ie. -x "python -v". // 执行的命令
-w, --watch dir........... watch directory "dir" or files. use once for // 监控文件夹
each directory or file to watch.
-i, --ignore ............. ignore specific files or directories. // 忽略特定的文件或目录
-V, --verbose ............ show detail on what is causing restarts. // 显示导致重新启动的详细信息
-- <your args> ........... to tell nodemon stop slurping arguments. // 告诉nodemon停止参数
Note: if the script is omitted, nodemon will try to read "main" from
package.json and without a nodemon.json, nodemon will monitor .js, .mjs, .coffee,
and .litcoffee by default.
For advanced nodemon configuration use nodemon.json: nodemon --help config
See also the sample: https://github.com/remy/nodemon/wiki/Sample-nodemon.json
Examples:
$ nodemon server.js
$ nodemon -w ../foo server.js apparg1 apparg2
$ nodemon --exec python app.py
$ nodemon --exec "make build" -e "styl hbs"
$ nodemon app.js -- --config # pass config to app.js
この時、「--configファイル」を観察しました。
コマンドを使用してnodemonの構成を変更することに加えて、nodemonの構成ファイルとして別のファイルを作成できることがわかりました。
4.nodemon.json ファイルを構成する
以下のディレクトリにnodemon.jsonファイルを作成します。
{
"watch": ["src"], // 监听src目录下文件变化
"ext": "ts", // 监控指定后缀名的文件
"ignore": ["src/**/*.spec.ts"], // 忽略的文件名后缀或文件夹
"exec": "node" // 当监控到变化时,自动执行的命令
}
コマンドラインを入力します:
nodemon --config nodemon
サービスを再起動すると、出力構成が変更されたことがわかります。
[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src
[nodemon] starting `node app.js`
5.小さな拡張
package.json 構成を変更します。
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && node app.js",
"dev": "nodemon" // npm run dev
}
コマンドラインで npm run dev と入力することは、nodemon を実行することと同じです。