forever、nodemon、pm2 和 supervisor 都是用于管理 Node.js 进程生命周期的工具,但它们的定位截然不同。nodemon 专为开发环境设计,通过监听文件变化自动重启服务以提升开发效率;pm2 是生产级进程管理器,提供集群模式、日志管理和进程守护;forever 是早期的保活工具,功能较为基础;supervisor 已停止维护,属于历史遗留方案。
在 Node.js 生态中,保持服务持续运行和在开发时自动重启是两个核心需求。forever、nodemon、pm2 和 supervisor 都试图解决这个问题,但它们的架构设计和适用场景有显著差异。作为架构师,我们需要根据环境(开发 vs 生产)和功能需求(保活 vs 集群)来做出选择。
这四个工具可以分为两类:开发工具和生产工具。
nodemon 纯粹为开发体验设计。
# nodemon: 启动开发服务器
npx nodemon app.js
pm2 是生产级进程管理器。
# pm2: 启动生产进程
pm2 start app.js --name "my-app" -i max
forever 是早期的保活工具。
# forever: 启动脚本保活
forever start app.js
supervisor 是历史遗留工具。
nodemon 取代。# supervisor: 启动(已不推荐)
supervisor app.js
理解它们何时重启进程至关重要。
nodemon 基于文件变更触发重启。
node_modules)。// nodemon.json 配置示例
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["src/tests"],
"exec": "ts-node ./index.ts"
}
pm2 基于进程状态触发重启。
--watch),但生产环境通常关闭此功能以防意外重启。// ecosystem.config.js 配置示例
module.exports = {
apps: [{
name: "app",
script: "./app.js",
watch: false, // 生产环境通常关闭 watch
max_memory_restart: "500M"
}]
};
forever 基于进程退出触发重启。
# forever: 仅保活,不监听文件变化
forever start -l forever.log -o out.log -e err.log app.js
supervisor 基于文件变更触发重启。
nodemon,但配置选项较少,灵活性差。# supervisor: 监听变化重启
supervisor --watch lib,config app.js
在生产环境中,单进程 Node.js 应用无法利用多核 CPU,且日志管理至关重要。
pm2 提供完整的集群模式。
# pm2: 开启集群模式,自动根据 CPU 核心数调整
pm2 start app.js -i max
# pm2: 查看日志
pm2 logs
forever 不支持原生集群模式。
# forever: 手动启动多个实例(需自行管理端口)
forever start -p 3000 app.js
forever start -p 3001 app.js
nodemon 不具备生产特性。
# nodemon: 无集群参数,仅单进程
nodemon app.js
supervisor 不具备生产特性。
# supervisor: 无相关生产参数
supervisor app.js
配置方式反映了工具的现代化程度。
pm2 支持声明式配置。
ecosystem.config.js 管理所有应用。// ecosystem.config.js
module.exports = {
apps: [{
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
};
nodemon 支持 JSON 配置。
nodemon.json 简单易读。// nodemon.json
{
"restartable": "rs",
"verbose": true
}
forever 主要依赖命令行参数。
# forever: 通过参数配置日志路径
forever start -l /var/log/forever.log app.js
supervisor 依赖命令行参数。
# supervisor: 通过参数配置
supervisor --no-restart-on path-change app.js
选择工具时,社区活跃度是长期稳定性的保障。
| 工具 | 状态 | 风险 | 建议 |
|---|---|---|---|
pm2 | ✅ 活跃维护 | 低 | 生产环境首选 |
nodemon | ✅ 活跃维护 | 低 | 开发环境首选 |
forever | ⚠️ 维护缓慢 | 中 | 仅限旧项目迁移 |
supervisor | ❌ 停止维护 | 高 | 避免使用 |
supervisor 的 npm 页面已明确标记为不再维护。使用它意味着你将无法获得安全更新,且可能面临与新版本 Node.js 的兼容性问题。
forever 虽然仍可运行,但功能迭代已停滞。对于需要现代运维特性(如 Docker 集成、云部署)的项目,pm2 是更稳妥的选择。
开发阶段:
毫不犹豫地选择 nodemon。它与 VS Code 等编辑器集成良好,配置简单,能显著提升编码效率。不要在生产流程中依赖它。
# package.json 脚本示例
"scripts": {
"dev": "nodemon src/index.js",
"start": "pm2 start ecosystem.config.js"
}
生产阶段:
必须选择 pm2。它的集群模式能显著提升吞吐量,守护机制确保服务高可用。配合 pm2 deploy 可以实现简单的零停机部署。
遗留系统:
如果遇到 forever 或 supervisor,制定迁移计划。supervisor 应优先替换为 nodemon。forever 应逐步迁移至 pm2 以获得更好的可观测性。
| 特性 | nodemon | pm2 | forever | supervisor |
|---|---|---|---|---|
| 主要场景 | 开发 | 生产 + 开发 | 生产 (旧) | 开发 (旧) |
| 文件监听 | ✅ 原生支持 | ✅ 可选 (--watch) | ❌ 不支持 | ✅ 原生支持 |
| 崩溃重启 | ✅ (视为文件变动) | ✅ 核心功能 | ✅ 核心功能 | ✅ (视为文件变动) |
| 集群模式 | ❌ | ✅ 支持 | ❌ | ❌ |
| 日志管理 | ❌ (输出到 stdout) | ✅ 高级管理 | ⚠️ 基础重定向 | ⚠️ 基础重定向 |
| 维护状态 | ✅ 活跃 | ✅ 活跃 | ⚠️ 缓慢 | ❌ 停止 |
在现代 Node.js 架构中,nodemon + pm2 是标准组合。开发时用 nodemon 享受热重载,部署时用 pm2 确保稳定运行。forever 和 supervisor 属于上一代工具链,除非维护旧系统,否则不应出现在新项目的依赖列表中。
不建议在任何新项目中选择 supervisor。该包已长期停止维护,存在未修复的安全隐患和兼容性问题。应迁移至 nodemon(开发)或 pm2(生产)以获得更好的支持和功能。
选择 forever 仅用于维护旧项目或极其简单的脚本保活。在新项目中不推荐使用,因为它的功能集不如 pm2 丰富,且社区活跃度较低。如果你的服务器环境非常受限且只需最基础的“挂了重启”,它仍可用。
选择 nodemon 用于本地开发环境。当你需要修改代码后自动重启 Node.js 服务,且不需要生产级的高可用或集群功能时,它是最佳选择。它配置简单,社区支持好,是前端开发者的标准开发工具。
选择 pm2 用于生产环境部署。当你需要确保应用崩溃后自动重启、利用多核 CPU 运行集群模式、或需要统一的日志管理和监控仪表盘时,pm2 是行业标准。它也支持开发模式,但主要优势在生产。
A little supervisor script for nodejs. It runs your program, and
watches for code changes, so you can have hot-code reloading-ish
behavior, without worrying about memory leaks and making sure you
clean up all the inter-module references, and without a whole new
require system.
Node Supervisor is used to restart programs when they crash.
It can also be used to restart programs when a *.js file changes.
Usage:
supervisor [options] <program>
supervisor [options] -- <program> [args ...]
Required:
<program>
The program to run.
Options:
-w|--watch <watchItems>
A comma-delimited list of folders or js files to watch for changes.
When a change to a js file occurs, reload the program
Default is '.'
-i|--ignore <ignoreItems>
A comma-delimited list of folders to ignore for changes.
No default
--ignore-symlinks
Ignore symlinks :)
-s|--timestamp
Log timestamp after each run.
Make it easy to tell when the task last ran.
-p|--poll-interval <milliseconds>
How often to poll watched files for changes.
Defaults to Node default.
-e|--extensions <extensions>
A comma-delimited list of file extensions to watch for changes.
Default is 'node,js' (or when CoffeeScript, 'node,js,coffee,litcoffee').
-x|--exec <executable>
The executable that runs the specified program.
Default is 'node'
-pid|--save-pid <path>
Save supervisor's process id to a file at the given path.
--debug[=port]
Start node with --debug flag.
--debug-brk[=port]
Start node with --debug-brk flag.
--harmony
Start node with --harmony flag.
--inspect
Start node with --inspect flag.
-n|--no-restart-on error|exit|success
Don't automatically restart the supervised program if it ends.
Supervisor will wait for a change in the source files.
If "error", an exit code of 0 will still restart.
If "exit", no restart regardless of exit code.
If "success", no restart only if exit code is 0.
-t|--non-interactive
Dissable interactive capacity
With this option, supervisor won't listen to stdin
--force-watch
Use fs.watch instead of fs.watchFile.
This may be useful if you see a high cpu load on a windows machine.
-k|--instant-kill
Instantly kills the server process, instead of gracefully shutting down the server.
This can be useful when the node app has events attached to SIGTERM or SIGINT so as to do a graceful shutdown before the process exits.
-RV|--restart-verbose
Logs the file(s) that caused supervisor to restart
-h|--help|-?
Display these usage instructions.
-q|--quiet
Suppress DEBUG messages
Options available after start:
rs - restart process. Useful when you want to restart your program even
if no file has changed.
Examples:
supervisor myapp.js
supervisor myapp.coffee
supervisor -w scripts -e myext -x myrunner myapp
supervisor -w lib,server.js,config.js server.js
supervisor -- server.js -h host -p port
In order to not watch for file changes, use "-i .".
Just run:
npm install supervisor -g
Get this code, and then do this:
npm link