缘由:开发一个node后端服务时,每次更改文件,均需重启一下,服务才能生效。使得开发效率降低了很多。
1.介绍
Nodemon是一个使用工具,它将会见监视源文件中任何的更改并自动重启服务器,我们开发时只需关注代码即可,不再需要手动重启服务。
Nodemon不会对你的代码产生额外的更改,它只是node命令的替代品。
2.安装
npm install -g nodemon
//或
npm install --save-dev nodemon
3.使用
nodemon ./main.js // 启动node服务
nodemon ./main.js localhost 6677 // 在本地6677端口启动node服务
4.script脚本配置
"start": "ts-node -r tsconfig-paths/register nodemon src/main.ts",
5.延迟重启
nodemon -delay10 main.js
nodemon --delay 2.5 server.js
nodemon --delay 2500ms server.js
4.命令行
nodemon
读命令行参数--->若无,查找本地local配置文件-->若无,查找全局配置文件->若无,尝试package.json中的main和start-->查找index.js
等价于配置文件
{
watch: [], // meaning all subdirectories
ignore: [],
script: 'index.js',
options: {
extensions: ['js'],
exec: 'node'
}
}
其option 转化逻辑为
--config 可设置指定配置文件
--ext 设置监听文件的后缀扩展名,如想要监听ts文件的变更,需设置-e ts
--exec 执行脚本
--watch 设置要监听的文件路径
--ignore 设置无需监听的文件路径
5.nodemon支持本地和全局配置文件
这些通常是命名的nodemon.json,可以位于当前工作目录或主目录中。可以使用该--config <file>选项指定备用本地配置文件。
配置文件nodemon.json
{
"verbose": true,
"ignore": ["*.test.js", "fixtures/*"], //忽视的文件
"execMap": {
"rb": "ruby",
"pde": "processing --sketch={{pwd}} --run"
}
}
也支持package.json配置,要同样格式且必须在nodemonConfig标签下:
"nodemonConfig":{
"ignore":["test/*","docs/*"]
}