Node.js development: nodemon --- the road to stepping on the pit (2)

The project is online today, and I can spare some time to continue working on node.js

I have been working on vue.js for a long time, subconsciously rely on hot reloading, ctrl+s to save! ! The page automatically presents the latest status! ! Simply cool! !

As a result, when working on node.js, ctrl+s to save, waiting for the latest status with anticipation, the result... eh? ? oh. . This is node.js and needs to enter the command

Only "node [startup file]" can be recompiled and started (disgusted face~)

So, I went to a certain degree to search for a wave, and found this thing - nodemon (automatic restart module)

--------------------------------------------------------------

process:

1. Installation dependencies

Global installation:

npm install -g nodemon // 8月23更正

Local installation:

npm install nodemon --save // 8月23更正

After the installation is complete, you can view the version through nodemon -v to determine whether the installation is successful

2. Start nodemon

Enter the command line:

nodemon

output:

[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`

3. About CLI options

Enter the command line:

nodemon -h

output:

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

At this time, I observed "--config file"

It turns out that in addition to modifying the configuration of nodemon through commands, I can create another file as the configuration file of nodemon

4. Configure the nodemon.json file

Create a nodemon.json file in the following directory

{
  "watch": ["src"], // 监听src目录下文件变化
  "ext": "ts", // 监控指定后缀名的文件
  "ignore": ["src/**/*.spec.ts"], // 忽略的文件名后缀或文件夹
  "exec": "node" // 当监控到变化时,自动执行的命令
}

Enter the command line:

nodemon --config nodemon

Restart the service and you will find that the output configuration has changed:

[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src
[nodemon] starting `node app.js`

5. Small expansion

Modify the package.json configuration:

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "tsc && node app.js",
    "dev": "nodemon" // npm run dev
  }

Entering npm run dev on the command line is equivalent to executing nodemon

Guess you like

Origin blog.csdn.net/qq_31808899/article/details/80520332