简体   繁体   English

如何让 npm 开始在不同的浏览器中打开

[英]How can I make npm start open into a different browser

On my pc the default browser is Firefox but I want to open my react projects on Chrome.在我的电脑上,默认浏览器是 Firefox,但我想在 Chrome 上打开我的 React 项目。 I tried changing the script in package.json as "start": "BROWSER=chrome react-scripts start" .我尝试将package.json中的脚本更改为"start": "BROWSER=chrome react-scripts start"
I also tried putting chrome inside single quotes.我还尝试将 chrome 放在单引号内。 I even tried creating a.env file as BROWSER=<my chrome path> and modified the package.json script to "start": "env-cmd react-scripts start" .我什至尝试将 a.env 文件创建为BROWSER=<my chrome path>并将package.json脚本修改为"start": "env-cmd react-scripts start"
I have also tried installing cross-env and using it.我也试过安装cross-env并使用它。 But none of this worked and npm start still opens a tab in Firefox. How can i fix this issue?但这些都不起作用, npm start仍会在 Firefox 中打开一个选项卡。我该如何解决这个问题?
React version: 18.2.0 OS: Windows 11反应版本:18.2.0 操作系统:Windows 11

So you want to override your default browser: navigate to package.json Replace: "start": "react-scripts start" to: "start": "BROWSER='google-chrome-stable' react-scripts start" for Linux like myself.所以你想覆盖你的默认浏览器:导航到package.json"start": "react-scripts start"替换为: "start": "BROWSER='google-chrome-stable' react-scripts start" Linux and "start": "BROWSER='chrome' react-scripts start" for windows and "start": "BROWSER='google chrome' react-scripts start" for OSx ."start": "BROWSER='chrome' react-scripts start"对于windows"start": "BROWSER='google chrome' react-scripts start"对于OSx Note that there is a " , ".请注意,有一个“ , ”。

if it works, there will be a response in the terminal.如果有效,终端中将有响应。

you@you:~/Your folder/your-app$ yarn start yarn run v1.22.19 $BROWSER='google-chrome-stable' react-scripts start

Terminal response终端响应

else: Run npm install --save-dev cross-env this is an npm install for cross-env since BROWSER is an environment variable.否则:运行npm install --save-dev cross-env这是 npm环境安装,因为BROWSER是一个环境变量。 Then navigate to package.json and edit "start": "BROWSER='google-chrome-stable' react-scripts start" to "start": "cross-env BROWSER=chrome react-scripts start" like so.然后导航到package.json并编辑"start": "BROWSER='google-chrome-stable' react-scripts start""start": "cross-env BROWSER=chrome react-scripts start"像这样。 Else: always run BROWSER=chrome npm start in place of npm start or simply open localhost:3000 or your current port on google chrome as long as the development server is running.否则:始终运行BROWSER=chrome npm start代替npm start或只要开发服务器正在运行,只需打开localhost:3000或谷歌浏览器上的当前端口。 All the best.祝一切顺利。

Else..... To set the default browser for your React projects in Visual Studio Code, you can try the following steps:否则.....要在 Visual Studio Code 中为您的 React 项目设置默认浏览器,您可以尝试以下步骤:

Open Visual Studio Code and go to the terminal.打开 Visual Studio Code 和 go 到终端。

In the terminal, enter the following command to install the open package, which allows you to specify the default browser for your React projects:在终端中,输入以下命令来安装打开的 package,它允许您为您的 React 项目指定默认浏览器:

npm install open

In the package.json file, update the start script to use the open package to open your React project in the desired browser, like this:在 package.json 文件中,更新启动脚本以使用打开的 package 在所需的浏览器中打开您的 React 项目,如下所示:

"scripts": {
  "start": "open -a /Applications/Google\\ Chrome.app -- react-scripts start"
}

Replace /Applications/Google\ Chrome.app with the path to the Chrome executable on your system.将 /Applications/Google\ Chrome.app 替换为系统上 Chrome 可执行文件的路径。 You can find this by opening Chrome, going to the menu, and selecting "About Google Chrome".您可以通过打开 Chrome、转到菜单并选择“关于 Google Chrome”来找到它。

Save the changes to package.json and run the npm start command in the terminal.保存对 package.json 的更改,然后在终端中运行 npm 启动命令。 This should open a new tab in Chrome with your React project.这应该会在 Chrome 中打开一个包含您的 React 项目的新选项卡。

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Windows 10 上使用“npm start”打开您想要的默认浏览器 - How to open the browser that you want as default with "npm start" on Windows 10 Node.js-当我运行“npm run start”或运行“mongod”时启动 MongoDB 时无法打开 React 浏览器 - Node.js- Can't get React browser to open when I run 'npm run start' or MongoDB to start when I run 'mongod' 如何在 npm 启动时禁用这些消息? - How can I disable these messages in the npm start? 如何让 npm 开始在 Chrome 上打开特定配置文件? - How do I get npm start to open a specific profile on Chrome? 如何使用代码 0 退出“npm run start”? - How do I make "npm run start" exit with a code 0? 我该如何解决这个错误v - npm start - npm ERR! 缺少脚本:开始 - How can I solve this errorv - npm start - npm ERR! missing script: start 为什么npm启动后我的浏览器打不开项目 - why does my browser does not open the project after npm start 如何使 npm start IP 地址与 php artisan serve 同步以在移动设备上使用后端功能? - How can I make the npm start IP address sync with php artisan serve to use backend functionality on mobile device? 安装 React 后如何修复此 npm 启动错误? - How can I fix this npm start error after installing React? 如何解决 npm/yarn start 问题? - How can I fix a a npm/yarn start issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM