Stop Create-React-App from opening the Browser on npm start

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# Table of Contents

  1. Stop Create-React-App from opening the Browser on npm start
  2. Open Create-React-App in a specific Browser on npm start

# Stop Create-React-App from opening the Browser on npm start

When you issue the npm start command, create-react-app opens your default browser.

If you want to need to stop create-react-app from opening a browser window on npm start, set the BROWSER environment variable to none.

shell
# for Windows, macOS and Linux npx cross-env BROWSER=none npm start

stop create react app from opening browser with env var

The command uses the cross-env package to set the BROWSER environment variable to none before issuing the npm start command.

If you are on macOS or Linux, you can also set the environment variable directly.

shell
# for macOS and Linux BROWSER=none npm start

setting the environment variable directly on macos and linux

The BROWSER environment variable can be set to different values (e.g. chrome or firefox) if you need to open your React App in a specific browser.

The values of the variable are platform-dependent.

OSBROWSER setting
Windowschrome
macOS"google chrome"
Linuxgoogle-chrome

You can also update the start command in your package.json file.

First, install the cross-env package.

shell
# with NPM npm install --save-dev cross-env # with YARN yarn add cross-env --dev

Now, update the start command in your package.json file.

package.json
{ "scripts": { "start": "cross-env BROWSER=none react-scripts start", }, "dependencies": {}, "devDependencies": {} }

updating your start script

# Using a .env file to prevent create-react-app from opening a browser window

You can also create a .env file in the root directory of your project (where your package.json file is) to set the BROWSER environment variable.

.env
BROWSER=none

disable browser open using env file

The next time you issue the npm start command, create-react-app won't open the browser in a new Window.

If you need to change the default port for create-react-app, check out my other article:

# Open Create-React-App in a specific Browser on npm start

You can also use the BROWSER environment variable if you need to open create-react-app in a specific browser when issuing the npm start command.

shell
# Windows npx cross-env BROWSER=chrome npm start # macOS npx cross-env BROWSER="google chrome" npm start # Linux npx cross-env BROWSER=google-chrome npm start # Or (also Linux) npx cross-env BROWSER=google-chrome-stable npm start

The values of the variable are platform-dependent.

OSBROWSER setting
Windowschrome
macOS"google chrome"
Linuxgoogle-chrome or google-chrome-stable

Some of the supported values include:

  • chrome
  • firefox
  • edge
  • browser (default web browser)
  • browserPrivate (default web browser in incognito mode)
  • none (don't open a browser on npm start)

You can also try BROWSER="firefox developer edition" if the string firefox doesn't work.

If you are on macOS or Linux, you can also set the BROWSER environment variable directly, without using cross-env.

shell
# macOS BROWSER="google chrome" npm start # Linux BROWSER=google-chrome npm start BROWSER=google-chrome-stable npm start

The same approach can be used to update the start command in your package.json file.

First, install the cross-env package.

shell
# with NPM npm install --save-dev cross-env # or with YARN yarn add cross-env --dev

Now, update the start command in your package.json file.

For Windows:

package.json
// Windows { "scripts": { "start": "cross-env BROWSER=chrome react-scripts start", }, "dependencies": {}, "devDependencies": {} }

For macOS:

package.json
// macOS { "scripts": { "start": "cross-env BROWSER='google chrome' react-scripts start", }, "dependencies": {}, "devDependencies": {} }

For Linux:

package.json
// Linux { "scripts": { "start": "cross-env BROWSER='google-chrome-stable' react-scripts start", }, "dependencies": {}, "devDependencies": {} }

Note: the value might also be google-chrome on Linux and not google-chrome-stable.

Create-react-app should open in the specified browser when you issue the npm start command.

# Using a .env file to open Create-React-App in a specific browser

You can also create a .env file in the root directory of your project (where your package.json file is) to set the BROWSER environment variable.

.env
BROWSER=chrome

open create react app in specific browser using env file

Make sure to specify the setting that corresponds to your operating system.

OSBROWSER setting
Windowschrome
macOS"google chrome"
Linuxgoogle-chrome or google-chrome-stable

Some of the supported values include:

  • chrome
  • firefox
  • edge
  • browser (default web browser)
  • browserPrivate (default web browser in incognito mode)
  • none (don't open a browser on npm start)

You can also try BROWSER="firefox developer edition" if the string firefox doesn't work.

If you need to change the default port for create-react-app, check out my other article:

If you have difficulties opening create-react-app in a different browser or preventing it from opening a new browser tab, try updating your create-react-app version.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.