create-react-app:我如何“npm start"?使用特定的浏览器? [英] create-react-app: How do I "npm start" with a specific browser?

查看:36
本文介绍了create-react-app:我如何“npm start"?使用特定的浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

npm 启动

在默认浏览器上启动反应服务器,对我来说是 Firefox.我喜欢用 Firefox 浏览,但更喜欢 Chrome 在 Web 开发中的开发工具.有没有办法强制npm start"使用 Chrome 启动服务器,而无需将我的默认浏览器更改为 chrome?我在 Windows 上使用 Bash.

starts the react server on the default browser, which is Firefox for me. I like Firefox for browsing but prefer Chrome in web development for its developer tools. Is there a way to force "npm start" to start the server with Chrome, without changing my default browser to chrome? I am using Bash on Windows.

我使用create-react-app"来创建我的服务器,这添加了一个 脚本到packages.json"文件用于npm start".该脚本使用默认浏览器启动本地主机服务器.如何修改create-react-app"添加的脚本,使其以不同的浏览器启动?

I used "create-react-app" to create my server and this adds a script to "packages.json" file for "npm start". The script starts the localhost server with the default browser. How do I modify the script added by "create-react-app" such that it starts with a different browser?

推荐答案

这可以通过 BROWSER 环境变量实现.

This is possible with the BROWSER environment variable.

您也可以直接在终端中进行:BROWSER=chrome npm start

You can also do it directly in the terminal: BROWSER=chrome npm start

这在高级配置文档中有描述:

默认情况下,Create React App 将打开默认系统浏览器,在 macOS 上优先使用 Chrome.指定浏览器以覆盖此行为,或将其设置为 none 以完全禁用它.如果您需要自定义浏览器的启动方式,则可以改为指定节点脚本.传递给 npm start 的任何参数也将传递给此脚本,并且提供应用程序的 url 将是最后一个参数.您脚本的文件名必须具有 .js 扩展名.

By default, Create React App will open the default system browser, favoring Chrome on macOS. Specify a browser to override this behavior, or set it to none to disable it completely. If you need to customize the way the browser is launched, you can specify a node script instead. Any arguments passed to npm start will also be passed to this script, and the url where your app is served will be the last argument. Your script's file name must have the .js extension.

还要注意浏览器名称在不同平台上是不同的:

Also note that the browser names are different on different platforms:

应用名称取决于平台.不要在可重用模块中对其进行硬编码.例如,Chrome 在 macOS 上是 google chrome,在 Linux 上是 google-chrome,在 Windows 上是 chrome.

The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is google chrome on macOS, google-chrome on Linux and chrome on Windows.

这篇关于create-react-app:我如何“npm start"?使用特定的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆