简体   繁体   中英

How can I make npm start open into a different browser

On my pc the default browser is Firefox but I want to open my react projects on Chrome. I tried changing the script in package.json as "start": "BROWSER=chrome react-scripts start" .
I also tried putting chrome inside single quotes. I even tried creating a.env file as BROWSER=<my chrome path> and modified the package.json script to "start": "env-cmd react-scripts start" .
I have also tried installing cross-env and using it. But none of this worked and npm start still opens a tab in Firefox. How can i fix this issue?
React version: 18.2.0 OS: Windows 11

So you want to override your default browser: navigate to package.json Replace: "start": "react-scripts start" to: "start": "BROWSER='google-chrome-stable' react-scripts start" for Linux like myself. and "start": "BROWSER='chrome' react-scripts start" for windows and "start": "BROWSER='google chrome' react-scripts start" for OSx . Note that there is a " , ".

if it works, there will be a response in the terminal.

you@you:~/Your folder/your-app$ yarn start yarn run v1.22.19 $BROWSER='google-chrome-stable' react-scripts start

Terminal response

else: Run npm install --save-dev cross-env this is an npm install for cross-env since BROWSER is an environment variable. Then navigate to package.json and edit "start": "BROWSER='google-chrome-stable' react-scripts start" to "start": "cross-env BROWSER=chrome react-scripts start" like so. Else: always run BROWSER=chrome npm start in place of npm start or simply open localhost:3000 or your current port on google chrome as long as the development server is running. All the best.

Else..... To set the default browser for your React projects in Visual Studio Code, you can try the following steps:

Open Visual Studio Code and go to the terminal.

In the terminal, enter the following command to install the open package, which allows you to specify the default browser for your React projects:

npm install open

In the package.json file, update the start script to use the open package to open your React project in the desired browser, like this:

"scripts": {
  "start": "open -a /Applications/Google\\ Chrome.app -- react-scripts start"
}

Replace /Applications/Google\ Chrome.app with the path to the Chrome executable on your system. You can find this by opening Chrome, going to the menu, and selecting "About Google Chrome".

Save the changes to package.json and run the npm start command in the terminal. This should open a new tab in Chrome with your React project.

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM