How to disable open browser in CRA?

ReactjsConfigurationCreate React-App

Reactjs Problem Overview


I've created a React app using create-react-app but whenever I start the dev server (using npm start), it opens up my browser. I want to make it so that it doesn't open my browser whenever I start my dev server.

How can I accomplish this?

Reactjs Solutions


Solution 1 - Reactjs

Create .env file in the root directory where your package.json file resides. And add the following:

BROWSER=none

Now run npm start.

Solution 2 - Reactjs

Add BROWSER=none to your npm start script in your package.json file, like this:

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

Check out the CRA documentation for more configuration and environment variable options:

https://create-react-app.dev/docs/advanced-configuration/


Update/Troubleshooting:

In case you're having a 'BROWSER' is not recognized as an internal or external command, operable program or batch file error: do an npm install of cross-env:

npm install --save cross-env

Then, add cross-env BROWSER=none to your start script

"start": "cross-env BROWSER=none react-scripts start",

Solution 3 - Reactjs

For Windows you can edit/create a script in package.json like this one:

"start": "set BROWSER=none && react-scripts start"

For Linux based OS, just delete the "set" and the "&&" and it will work:

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

Solution 4 - Reactjs

I suggest doing it at the command level, so you don't have to change any files that get committed.

BROWSER=none npm start

You can add an alias for this to your shell's configuration:

alias myapp-start='cd /path/to/myapp && BROWSER=none npm start'

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
QuestionGSP KSView Question on Stackoverflow
Solution 1 - Reactjsuser9441203View Answer on Stackoverflow
Solution 2 - ReactjsBassemView Answer on Stackoverflow
Solution 3 - ReactjsFrank GarciaView Answer on Stackoverflow
Solution 4 - ReactjsBenjamin AtkinView Answer on Stackoverflow