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

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

问题描述

npm开始

在默认浏览器(对我来说是Firefox)上启动react服务器.我喜欢Firefox来浏览,但是更喜欢在Web开发中使用Chrome作为其开发人员工具.有没有一种方法可以强制"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"创建服务器,这添加了.该脚本使用默认浏览器启动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上是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天全站免登陆