0%

stop opening browser in CRA app

begin

There is a feature in webpack devServer configuration which is called open. When the open is configured as true, the application will open the browser automatically. I don’t think it’s good for me while coding. In particular, I will restart the application often during the coding time. So I want to disable this action in CRA-based project.

How to fix

We know that CRA-based project is hard to change the webpack config. So I choose to install the react-app-rewired and custom-cra to config the webpack myself. Unfortunately, the way to override devServer config isn’t able to change the open. I can’t see the open option when I log the config property. And it doesn’t work when I write the open: false to the override config. So I search in Bing.

There is an article about how to stop the action. It also doesn’t work for me. Maybe because my operation system is Windows 10. I can’t set the environment variable in command line.

There is an Issue in create-react-app repository. The first answer said it’s an option in custom-react-scripts, but I can’t find the way to fix the problem so I abandoned.

Finally, I find a solution in stack-overflow. The answer said I can write the environment variable in .env file and yarn start or npm run start can read it. That works for me.