我如何“npm开始”使用特定的浏览器? [英] How do I "npm start" with a specific browser?

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

问题描述


npm start

npm start

在默认浏览器上启动react服务器,这对我来说就是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。该脚本使用默认浏览器启动localhost服务器。如何修改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上的谷歌浏览器,Linux上的谷歌浏览器和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.

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

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