85

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?

Osama Qarem
  • 1,049
  • 1
  • 9
  • 14
  • Possible duplicate of [How to use nodejs to open default browser and navigate to a specific URL](https://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url) – Mark C. Aug 07 '18 at 01:53
  • @MarkC. Sorry I didn't have a proper understanding of my problem before posting the question. I will explain what I want more properly now. I used "create-react-app" to create my server and this adds a [script to "packages.json" file for "npm start"](https://i.imgur.com/j8DLTWU.png). 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? – Osama Qarem Aug 08 '18 at 02:39
  • Maybe you can `spawn` Google Chrome with your URL? – NoobTW Aug 08 '18 at 03:02
  • You can also use `opn http://localhost:3000 -- 'google chrome'` with `opn-cli` https://github.com/sindresorhus/opn-cli – NoobTW Aug 08 '18 at 03:07
  • 1
    shall we edit the question title to mention 'create-react-app'? – dcorking Dec 13 '19 at 11:57
  • 1
    @dcorking Didn't realize I could edit the title. Changed, thanks! – Osama Qarem Dec 14 '19 at 14:29
  • @MarkC. - it may be a duplicate in the technical low-level sense, but it's worth keeping this question for reference, as it serves the react.js community (where most of the time, the dev workflows are structured, boilerplate code exists, and other configuration related considerations arise as a result, like whether to eject a react app or not). – Eliran Malka Dec 22 '21 at 21:39
  • @EliranMalka this is a 3 year old thread... – Mark C. Dec 22 '21 at 22:10
  • @MarkC. - yes, i tend to get nostalgic like that :) JK i didn't notice.. – Eliran Malka Dec 23 '21 at 00:42

17 Answers17

80

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.

Abbe
  • 1,603
  • 1
  • 23
  • 30
46

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"
    
n0noob
  • 753
  • 6
  • 22
  • 7
    I think the Windows syntax is wrong. I get `'BROWSER' is not recognized as an internal or external command, operable program or batch file.` – Ron Inbar Jun 22 '20 at 17:46
  • 2
    @RonInbar Probably you need to specify full path of your `chrome` executable in windows. – n0noob Jul 05 '20 at 14:41
  • 1
    With remote debugging enabled: `"start": "BROWSER='google chrome' BROWSER_ARGS='--remote-debugging-port=9222' react-scripts start"` – zrkl Aug 18 '21 at 15:25
  • 1
    This worked great. Thanks. – Nunchuk Feb 02 '22 at 22:03
  • 1
    Not working in React 18.0.0. Is working in 17.0.1. Actually need `"react": "^17.0.1", "react-scripts": "4.0.1"` Any fixes for 18.0.0 ? – brunshte Apr 13 '22 at 21:04
23

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

BROWSER="firefox developer edition"
Vlad
  • 5,633
  • 1
  • 59
  • 67
  • 4
    This actually doesn't work for me (on a Windows machine currently), but using `BROWSER="firefox"` still opens Firefox Developer Edition so I guess I'm good – gkri Aug 30 '19 at 08:27
  • 4
    It's a rather personal preference, so you might want to add it to `.env.local` (which is ignored by git) – Rahel Lüthy Apr 14 '20 at 07:38
  • Simple working solution, working with macOS catalina – Feras Sep 05 '20 at 04:29
  • `BROWSER=firefox developer edition` works for me on Big Sur, though previously this was `BROWSER=Firefox Developer Edition` . Best try both. No quote nor full path did work. – Romain Champourlier Jan 13 '21 at 16:39
22

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.

SherylHohman
  • 14,460
  • 16
  • 79
  • 88
UTKARSH SINHA
  • 221
  • 2
  • 2
5

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!

mohammad
  • 51
  • 1
  • 3
4

In Windows cmd, set env variable for desired browswer:

set BROWSWER=chrome

Then just run npm start like normal

mrpaz
  • 73
  • 4
4

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.

daGo
  • 2,140
  • 21
  • 21
  • That's what I was thinking. The requirement that I constantly have to add this variable every time I create a react project is unreasonable. At worst it should be a switch in the npm start. – Ken Ingram Feb 15 '21 at 04:19
2

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

Ankit Tiwari
  • 3,333
  • 3
  • 9
  • 31
2

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"
RobC
  • 20,007
  • 20
  • 62
  • 73
1

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!

Narendra Solanki
  • 868
  • 12
  • 20
1

This is how I solved mine:

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

dippas
  • 52,735
  • 15
  • 110
  • 120
1

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.

1

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.

dkocich
  • 221
  • 4
  • 7
0

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?

brunshte
  • 109
  • 7
-1

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.

wscourge
  • 9,289
  • 12
  • 51
  • 72
Srinivasan N
  • 613
  • 8
  • 8
-2

Add script to your package.json file

"devserver": "live-server --browser=Chrome"
lem
  • 13
  • 3
-2

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

Gk Mohammad Emon
  • 4,390
  • 2
  • 35
  • 34