如何关闭react
关闭 React 开发服务器
在开发过程中,React 应用通常通过 npm start 或 yarn start 启动开发服务器。要关闭服务器,可以直接在运行服务器的终端窗口中按下 Ctrl + C(Windows/Linux)或 Command + C(Mac)。系统会提示是否终止批处理操作,输入 Y 并回车确认。
终止后台运行的 React 进程
如果开发服务器在后台运行或终端窗口已关闭,可以通过以下命令终止进程:
-
查找占用端口的进程(默认端口为 3000):

lsof -i :3000或使用
netstat(Windows):netstat -ano | findstr :3000 -
终止进程:

- Linux/Mac:
kill -9 <PID> - Windows:
taskkill /PID <PID> /F
- Linux/Mac:
清理缓存和依赖
如果需要彻底关闭并清理 React 项目环境,可以删除 node_modules 和构建文件:
rm -rf node_modules build
然后重新安装依赖:
npm install
关闭生产环境服务
对于生产环境部署的 React 应用(如通过 serve 或 nginx 运行):
- 使用
pm2管理的进程:pm2 stop <app_name> nginx服务:sudo systemctl stop nginx
防止自动重启
某些工具(如 nodemon)会监听文件变化并自动重启。确保配置文件中未启用类似功能,或在启动时添加 --ignore 参数。






