You can add the environment variable directly into the scripts section of your package. json file. Notice that we also give the BROWSER environment variable a value of none . This is what tells Create-React-App to not open any browser.
The solution is to set the environment variable BROWSER to either "none" or "chrome" or whatever browser you need. In order to set it in the same Powershell terminal, use $Env:BROWSER="none" . Now running npm start will not open a new browser window/tab.
But the eject command comes with a price. Once you eject, you can't go back and hide the configuration files. You will have to maintain your React app configuration on your own. This means: You need to update the dependencies and ensure its not broken when a new version is released.
Create .env
file in the root directory where your package.json
file resides. And add the following:
BROWSER=none
Now run npm start
.
Hope it helps :)
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",
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With