nodemon 是一个 Node.js 开发工具,用于监听文件变化并自动重启应用。它可以在你修改代码后自动重启 Node 进程,无需手动停止和重新运行。
简单来说,nodemon 解决的是 Node.js 开发中最常见的痛点:改了代码就要 Ctrl+C 然后重新 node index.js。
一、为什么需要 nodemon?
没有 nodemon 的工作流
# 1. 启动应用
node index.js
# 2. 修改代码后
Ctrl+C
# 3. 重新启动
node index.js
# 每次修改都要重复 2-3 步,非常低效
使用 nodemon 后
# 启动(一次性)
nodemon index.js
# 修改代码后自动重启,无需任何操作
# [nodemon] restarting due to changes...
# [nodemon] starting `node index.js`
二、基本用法
1. 安装
# 全局安装(推荐,所有项目可用)
npm install -g nodemon
# 或项目内安装(作为开发依赖)
npm install -D nodemon
2. 基本命令
# 启动应用并监听文件变化
nodemon index.js
# 指定入口文件
nodemon src/index.js
# 运行 TypeScript 文件
nodemon src/index.ts
# 指定要监听的文件扩展名
nodemon --ext js,json,html index.js
# 忽略某些文件/目录
nodemon --ignore dist/ --ignore test/ index.js
三、常用配置方式
方式1:命令行参数
# 监听特定目录
nodemon --watch src --watch config index.js
# 延迟重启(毫秒)
nodemon --delay 500 index.js
# 显示详细启动信息
nodemon --verbose index.js
# 不监听 node_modules
nodemon --ignore node_modules/ index.js
方式2:package.json 中配置
{
"scripts": {
"dev": "nodemon src/index.js",
"dev:watch": "nodemon --watch src --ext js,json src/index.js"
}
}
方式3:nodemon.json 配置文件(推荐)
在项目根目录创建 nodemon.json:
{
"watch": ["src", "config"],
"ext": "js,json,ts",
"ignore": ["src/**/*.test.js", "node_modules", "dist"],
"delay": 1000,
"verbose": true,
"exec": "node --inspect",
"env": {
"NODE_ENV": "development",
"PORT": 3000
},
"events": {
"restart": "echo '应用已重启'"
}
}
然后在 package.json 中:
{
"scripts": {
"dev": "nodemon"
}
}
四、高级用法
1. 配合 TypeScript
# 方法1:直接运行 ts 文件
nodemon src/index.ts
# 方法2:指定执行命令
nodemon --exec ts-node src/index.ts
# 方法3:nodemon.json 配置
{
"exec": "ts-node",
"watch": ["src"],
"ext": "ts"
}
2. 配合调试工具
# 开启 Node.js 调试端口
nodemon --inspect index.js
# 或指定端口
nodemon --inspect=9229 index.js
3. 自定义重启事件
{
"events": {
"start": "echo '应用启动'",
"restart": "echo '应用重启'",
"crash": "echo '应用崩溃'"
}
}
4. 支持的环境变量
# 设置环境变量
nodemon --env .env index.js
# 或在 nodemon.json 中配置
{
"env": {
"NODE_ENV": "development",
"DB_URL": "mongodb://localhost:27017/test"
}
}
五、常用场景示例
场景1:Express 开发
// package.json
{
"scripts": {
"dev": "nodemon server.js"
}
}
// server.js
import express from 'express';
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.json({ message: 'Hello World' });
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
场景2:微服务开发
{
"scripts": {
"dev:user": "nodemon --watch services/user services/user/index.js",
"dev:order": "nodemon --watch services/order services/order/index.js",
"dev:all": "concurrently \"npm run dev:user\" \"npm run dev:order\""
}
}
场景3:前端构建工具
{
"scripts": {
"build:watch": "nodemon --watch src --exec 'npm run build' --ext js,jsx"
}
}
六、nodemon vs Node.js --watch
Node.js 从 v18.11.0 开始内置了 --watch 模式,部分场景可以替代 nodemon:
| 特性 | nodemon | Node.js --watch |
|---|---|---|
| 启动方式 | nodemon index.js | node --watch index.js |
| 配置文件 | ✅ nodemon.json | ❌ 不支持 |
| 忽略文件 | ✅ 灵活配置 | ❌ 基础支持 |
| 自定义脚本 | ✅ exec 配置 | ❌ 不支持 |
| 延迟重启 | ✅ delay | ❌ 不支持 |
| 扩展名过滤 | ✅ ext 配置 | ⚠️ 有限支持 |
| 环境变量 | ✅ env 配置 | ⚠️ 需手动设置 |
| 版本要求 | 任何版本 | Node 18.11+ |
# Node.js 内置 watch(基础版)
node --watch index.js
# nodemon(增强版)
nodemon index.js
选择建议:
- 简单项目、Node 20+ → 可以用
node --watch - 需要自定义配置、复杂项目 → 推荐 nodemon
七、常见问题
Q: nodemon 无法重启?
A: 检查:
- 是否正确安装?
nodemon -v - 监听路径是否正确?
nodemon --watch src - 是否有忽略文件冲突?
--ignore配置
Q: 修改文件后不重启?
A: 可能原因:
- 文件在
ignore列表中 - 文件扩展名不在
ext中 - 文件变化太快,
delay延迟未触发
Q: 如何查看 nodemon 在监听哪些文件?
nodemon --verbose index.js
# 会输出 [nodemon] watching: src/**/*, config/**/*
Q: 如何重启 nodemon 手动?
# 在运行 nodemon 的终端中输入
rs
Q: nodemon 和 pm2 的区别?
| 工具 | 用途 | 场景 |
|---|---|---|
| nodemon | 开发时自动重启 | 本地开发 |
| pm2 | 生产环境进程管理 | 线上部署、集群、守护进程 |
八、最佳实践
1. 项目级配置
// nodemon.json
{
"watch": ["src"],
"ignore": ["src/**/*.test.js", "src/**/*.spec.js", "node_modules"],
"ext": "js,json,ts",
"exec": "node --inspect=9229",
"delay": 500,
"env": {
"NODE_ENV": "development"
}
}
2. package.json 脚本
{
"scripts": {
"dev": "nodemon",
"dev:debug": "nodemon --inspect-brk",
"dev:ts": "nodemon --exec ts-node src/index.ts"
}
}
3. 配合调试器
# Chrome DevTools 调试
nodemon --inspect index.js
# 然后打开 chrome://inspect
九、总结
| 特性 | 说明 |
|---|---|
| 核心作用 | 文件变化时自动重启 Node 应用 |
| 适用场景 | Node.js 本地开发 |
| 安装方式 | 全局 npm i -g nodemon 或项目内 npm i -D nodemon |
| 基本用法 | nodemon index.js |
| 配置方式 | 命令行参数、package.json、nodemon.json |
| 核心优势 | 提升开发效率,自动重启,配置灵活 |
| 替代方案 | Node.js 20+ 内置 --watch(功能较基础) |
一句话总结:nodemon 是 Node.js 开发的效率神器,它让你专注于写代码,而不是反复手动重启服务。