create-react-app: How do I “npm start” with a specific browser?

后端 未结 11 1943
一个人的身影
一个人的身影 2021-01-31 07:11

npm start

starts the react server on the default browser, which is Firefox for me. I like Firefox for browsing but prefer Chrome in web deve

相关标签:
11条回答
  • 2021-01-31 07:51

    This is how I solved mine:

    I opened the application on vsCode, then via the terminal I ran "BROWSER=Chrome npm start".

    0 讨论(0)
  • 2021-01-31 07:52

    There is one package called set-default-browser https://www.npmjs.com/package/set-default-browser

    just download package from there and add following code

    var setDefaultBrowser = require('set-default-browser');
    
    setDefaultBrowser("chrome");
    

    Or you can just run this set-default-browser chrome

    Thanks!

    0 讨论(0)
  • 2021-01-31 07:56

    This is possible with the BROWSER environment variable.

    You can also do it directly in the terminal: BROWSER=chrome npm start

    This is described in the Advanced Configuration docs:

    By default, Create React App will open the default system browser, favoring Chrome on macOS. Specify a browser to override this behavior, or set it to none to disable it completely. If you need to customize the way the browser is launched, you can specify a node script instead. Any arguments passed to npm start will also be passed to this script, and the url where your app is served will be the last argument. Your script's file name must have the .js extension.

    Also note that the browser names are different on different platforms:

    The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is google chrome on macOS, google-chrome on Linux and chrome on Windows.

    0 讨论(0)
  • 2021-01-31 07:57

    As you have mentioned that you are using create-react-app for creating react app and you want chrome to open on hitting npm start. Set BROWSER variable in package.json present in your project in the following manner:

    Replace:

    "start": "react-scripts start"
    

    With:

    • Linux:
      "start": "BROWSER='google-chrome-stable' react-scripts start"
      
    • Windows:
      "start": "BROWSER='chrome' react-scripts start"
      
    • OS X:
      "start": "BROWSER='google chrome' react-scripts start"
      
    0 讨论(0)
  • 2021-01-31 07:58

    Add script to your package.json file

    "devserver": "live-server --browser=Chrome"
    
    0 讨论(0)
  • 2021-01-31 07:59

    In Windows cmd, set env variable for desired browswer:

    set BROWSWER=chrome

    Then just run npm start like normal

    0 讨论(0)
提交回复
热议问题