create-react-app: how to use https instead of http?

后端 未结 19 1120
眼角桃花
眼角桃花 2020-12-24 10:04

I was wondering if anyone knows how to use https on dev for the \'create-react-app\' environment. I can\'t see anything about that in the README or quick googling. I just wa

相关标签:
19条回答
  • 2020-12-24 11:05

    I`m using Windows 10 and I had the same issue. I realized that you need to:

    1. Run Command Prompt with Administrator Privileges
    2. Run on the terminal bash this command: set HTTPS=true&&npm start
    3. You can also put this code into your package.json file under the scripts section like this:

      "scripts": { "start": "set HTTPS=true&&react-scripts start", (...) }

    4. Bonus: If you want to change the PORT use this command insted:

      set HTTPS=true&&set PORT=443&&react-scripts start

      Obs.: Pay attention to the blank spaces NOT left in between some characters.

    You can browse this link for more detais.

    0 讨论(0)
  • 2020-12-24 11:07

    Please use this in command prompt

     set HTTPS=true&&npm start
    
    0 讨论(0)
  • 2020-12-24 11:09

    To avoid untrusted certificate errors in Chrome and Safari you should manually specify a self-signed key pair. CRA allows you to specify them.

    Also, use .env file to store these vars.

    On macOS, just add your certificate to Keychain Access and then set Trust Always in its details.

    0 讨论(0)
  • 2020-12-24 11:10

    You can also create a file called .env in the root of your project, then write

    HTTPS=true
    

    After that, just run "npm start" as you usually do to start your app.

    Docs: https://facebook.github.io/create-react-app/docs/advanced-configuration

    Works both on Linux and Windows, unlike some other answers posted here.

    0 讨论(0)
  • 2020-12-24 11:10

    In Case of MAC/UNIX do

    export HTTPS=true
    npm start
    

    Or simple one liner

    export HTTPS=true&&npm start
    

    Or update start script in package.json to

    "start": "export HTTPS=true&&PORT=3000 react-scripts start",

    you should be able to hit https.

    0 讨论(0)
  • 2020-12-24 11:10

    In Windows environment add following lines to package.json:

      "scripts": {
        "start-dev": "set HTTPS=true&&set PORT=443&&react-scripts start"
      },
    

    It will start development server with https and port 443. At the present moment NodeJs have known bug to run this correctly but it worked with nodeJs v8.11.3 - https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi for me.

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