create-react-app: How do I "npm start" with a specific browser?

Javascriptnode.jsWindowsNpm

Javascript Problem Overview


> npm start

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.

Edit: 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?

Javascript Solutions


Solution 1 - Javascript

This is possible with the BROWSER environment variable.

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

This is described in the Advanced Configuration docs:

> 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:

> 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.

Solution 2 - Javascript

As you have mentioned that you are using create-react-app for creating react app and you want chrome to open on hitting npm start. Set BROWSER variable in package.json present in your project in the following manner:

Replace:

"start": "react-scripts start"

With:

  • Linux:
    "start": "BROWSER='google-chrome-stable' react-scripts start"
    
  • Windows:
    "start": "BROWSER='chrome' react-scripts start"
    
  • OS X:
    "start": "BROWSER='google chrome' react-scripts start"
    

Solution 3 - Javascript

Method by using .env file in the root of your NodeJS app.

BROWSER="firefox developer edition"

Solution 4 - Javascript

Using above technique, you may end up with error 'BROWSER' is not recognized as an internal or external command, operable program or batch file.

To over come this Do an npm install of cross-env in your cloned repo: npm install --save cross-env

Try to use this command in the package.json file

"start": "cross-env BROWSER=chrome react-scripts start"

BROWSER is an environment variable, and you can use the cross-env package to properly handle it.

Solution 5 - Javascript

Simply add the env-cmd package as global

then create a .env file and write a variable with a specific Browsers path after that add the env-cmd just in your start script

in the terminal

npm install -g env-cmd

in the .env file

BROWSER= "your browser path"

like => BROWSER= "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge"

in the package.json add the env-cmd

"scripts": {
   "start": "env-cmd react-scripts start",
   "build": "react-scripts build",
   "test": "react-scripts test",
   "eject": "react-scripts eject"
}

that should work!

Solution 6 - Javascript

In Windows cmd, set env variable for desired browswer:

set BROWSWER=chrome

Then just run npm start like normal

Solution 7 - Javascript

I don't like to repeatedly create a new .env file or prepend npm start every time with an additional command. You can specify your browser of choice instead of none in your shell config file. Type in your terminal the following commands:

echo export BROWSER=none >> ~/.bashrc
source ~/.bashrc

At this point you can run npm start and be happy.

Solution 8 - Javascript

for Brave browser it is BROWSER=brave-browser npm start

Solution 9 - Javascript

Change your default Browser setting in windows, if it does not work then open your visual studio code and change the script browser to:

"start": "BROWSER=chrome react-scripts start"

Solution 10 - Javascript

There is one package called set-default-browser https://www.npmjs.com/package/set-default-browser

just download package from there and add following code

var setDefaultBrowser = require('set-default-browser');
 
setDefaultBrowser("chrome");

Or you can just run this set-default-browser chrome

Thanks!

Solution 11 - Javascript

This is how I solved mine:

I opened the application on vsCode, then via the terminal I ran "BROWSER=Chrome npm start".

Solution 12 - Javascript

If you want to change the default browser when you are running a npm start or yarn start, the simplest way to do that is edit your package.json file.

Many are not comfortable dealing with environment variables using the terminal.

This is what your scripts section should look like:

"scripts": {
"start": "BROWSER=none react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},

In the above scenario it would not open any browser at all, you are free to choose your development browser and continue your work(I prefer this one). However, if you want a specific browser then you can replace BROWSER=none with any of the following:

  • BROWSER=firefox
  • BROWSER=google-chrome-stable
  • BROWSER=vivaldi

Suit yourself.

Solution 13 - Javascript

on windows, the easies way with create-react-app was to add BROWSER="C:\Program Files\Google\Chrome Dev\Application\chrome.exe" to my .env.developmennt.local file in each CRA project. I use a different browser without dev extensions as the default one set in the system.

Solution 14 - Javascript

On Mac, this method:

"start": "BROWSER='firefox developer edition' react-scripts start"

works on 'react': '17.0.1' together with 'react-scripts': '4.0.1'

But it is not working on 'react': '18.0.1' together with 'react-scripts': '5.0.1'.

On 18.0.1 it continues to open in the default browser set on my computer. So I have sometimes reverted back to using the older React version but do not want to continue doing this as I do need the newer version in some cases and it's just easier to install the most recent version using create-react-app.

Any suggestions?

Solution 15 - Javascript

To open in chrome we need to set it as a default browser.

Setting --> Default browser --> Make default -->

and choose chrome if any other browser is chosen.

It worked on windows 10.

Solution 16 - Javascript

Add script to your package.json file

"devserver": "live-server --browser=Chrome"

Solution 17 - Javascript

If you are a Windows user then go to Sitting -> Default apps -> Web browser then select your desired browser.If you are a Linux user then go to System-settings -> Details. (Note: In older versions of Ubuntu Details is called System Info)

Sitting of windows

Sitting of linux

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOsama QaremView Question on Stackoverflow
Solution 1 - JavascriptAbbeView Answer on Stackoverflow
Solution 2 - Javascriptn0noobView Answer on Stackoverflow
Solution 3 - JavascriptVladView Answer on Stackoverflow
Solution 4 - JavascriptUTKARSH SINHAView Answer on Stackoverflow
Solution 5 - JavascriptmohammadView Answer on Stackoverflow
Solution 6 - JavascriptmrpazView Answer on Stackoverflow
Solution 7 - JavascriptdaGoView Answer on Stackoverflow
Solution 8 - JavascriptAnkit TiwariView Answer on Stackoverflow
Solution 9 - Javascriptdeveloperrakib View Answer on Stackoverflow
Solution 10 - JavascriptNarendra SolankiView Answer on Stackoverflow
Solution 11 - JavascriptUche IgbokweView Answer on Stackoverflow
Solution 12 - JavascriptDebojyoti ChatterjeeView Answer on Stackoverflow
Solution 13 - JavascriptdkocichView Answer on Stackoverflow
Solution 14 - JavascriptbrunshteView Answer on Stackoverflow
Solution 15 - JavascriptSrinivasan NView Answer on Stackoverflow
Solution 16 - JavascriptlemView Answer on Stackoverflow
Solution 17 - JavascriptGk Mohammad EmonView Answer on Stackoverflow