如何查看react项目端口
查看 React 项目端口的方法
通过 package.json 文件查看
React 项目的端口通常在 package.json 文件的 scripts 部分定义,例如:
"scripts": {
"start": "react-scripts start --port 3000"
}
如果未指定 --port,默认端口通常是 3000。
通过开发服务器启动日志查看
运行 npm start 或 yarn start 后,终端会显示开发服务器的启动信息,其中包含类似以下内容:
You can now view your app in the browser.
Local: http://localhost:3000
通过环境变量配置端口
可以在项目根目录的 .env 文件中设置自定义端口:
PORT=4000
重启开发服务器后,应用将使用新端口。
通过命令行参数临时修改端口
启动时直接指定端口:
npm start --port 5000
或
yarn start --port 5000
检查进程占用端口(Linux/macOS)
如果端口被占用或需要确认,可通过以下命令查看:
lsof -i :3000
或使用 netstat(Windows):
netstat -ano | findstr :3000





