方法一:修改 package.json 脚本(推荐)
在启动命令中显式指定 Chrome 路径。以下是针对不同系统的配置示例:
javascript
{
"scripts": {
"start": "react-scripts start && open -a 'Google Chrome' http://localhost:3000" // macOS
// 或 Windows 系统:
// "start": "react-scripts start && start chrome http://localhost:3000"
// 或 Linux 系统:
// "start": "react-scripts start && google-chrome http://localhost:3000"
}
}
说明:通过 open -a 'Google Chrome'
(macOS)或 start chrome
(Windows)强制使用 Chrome 打开 URL。
方法二:配置 IDEA 的 JavaScript Debug
- 打开运行配置 :
点击 Run/Debug Configurations → + → JavaScript Debug。 - 设置 URL :
在 URL 中输入http://localhost:3000
。 - 添加浏览器参数 :
在 Additional arguments 中添加--browser=chrome
(部分插件支持)。 - 配置 Before launch :
确保启动前执行npm start
或等效命令。
方法三:使用 cross-env 和 open 包(跨平台方案)
-
安装依赖:
bashnpm install cross-env open --save-dev
-
修改 package.json:
bash{ "scripts": { "start": "react-scripts start && cross-env BROWSER=chrome open http://localhost:3000" } }
方法四:设置系统默认浏览器
若上述方法无效,可直接修改系统默认浏览器:
- Windows :
设置 → 应用 → 默认应用 → 网页浏览器 → 选择 Chrome。 - macOS :
系统偏好设置 → 通用 → 默认网页浏览器 → 选择 Chrome。
验证配置
- 在 IDEA 中运行
npm start
脚本。 - 若 Chrome 仍未启动,检查:
- Chrome 是否安装在默认路径。
- 命令中的路径是否与实际安装位置一致(如 Windows 可能需要完整路径)。
通过以上配置,可确保项目启动时优先使用 Chrome 打开。