124

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 want either the https://localhost:3000 to work, or else https://localhost:3001.

Ringo
  • 5,097
  • 3
  • 31
  • 46

24 Answers24

131

Set HTTPS=true before you run the start command.

Documentation

The implementation uses the HTTPS Environment Variable to determine which protocol to use when starting the server.

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
  • 3
    I tried this. I'm on Windows 10 and ran: HTTPS=true&&npm start This did not seem to work. I got the following console message as usual: Compiled successfully! You can now view x-app in the browser. Local: http://localhost:3000/ On Your Network: http://192.168.2.4:3000/ – Ringo Jun 15 '17 at 18:34
  • seems like your suggestion has something to do with proxying calls to a secure API server? I know react-scripts just calls something like http-server. I might be able to figure it out by looking at the output – Ringo Jun 15 '17 at 18:36
  • 11
    Did you just run `HTTPS=true&&npm start`, on windows you need `set HTTPS=true&&npm start`. – Steve Buzonas Jun 15 '17 at 18:46
  • The HTTPS section of the documentation is after Proxy Configuration, but the same header level. They are separate features. – Steve Buzonas Jun 15 '17 at 18:53
  • It doesn't call `http.server` by itself, it uses `webpack-dev-server` which uses `express`. – Steve Buzonas Jun 15 '17 at 19:01
  • OK, it works now. Is there a script where i can add the HTTPS env without having to type it everytime.(And also be compatible for Mac as well as PC – Ringo Jun 15 '17 at 20:03
  • 2
    On a unix system like Mac you can export it in your profile. There's an equivalent way to globally set environment variables on windows, but I'm not familiar with it. But, at the project level `dotenv` should be initialized before the start, so you can try adding `HTTPS=true` to your `.env.development` – Steve Buzonas Jun 15 '17 at 20:20
  • @SteveBuzonas et al., has anyone figured out how to define your own cert? In Firefox I can't proceed on https://localhost due to a 'The certificate is not trusted because it is self-signed.' error. – Sean Nov 28 '17 at 18:43
  • @Sean you would need to fork `react-scripts` or `eject` in order to do that. You can use a predefined cert with `webpack-dev-server`, but the scripts just allow it to generate a cert. Why not just add an exception? – Steve Buzonas Nov 30 '17 at 17:39
  • 2
    @Ringo A little late to the party, but [this shows how to set `PATH` for Java](https://www.java.com/en/download/help/path.xml). It's the same process for any environment variable, like `HTTPS` or `NODE_ENV` or what-have-you on Windows. You can also open Windows Explorer, right click *This PC*, select *Properties*, then *Advanced System Settings*, then click the *Environment Variables* button... & profit. – ruffin May 09 '18 at 15:03
  • @Sean You can pass in custom certificates with the `SSL_CRT_FILE` and `SSL_KEY_FILE` env vars and CRA will use them instead of the default ones from webpack. To have HTTPS working on localhost without the warning, you can use mkcert to create your own Certificate Authority and generate certificates with it. It sounds more complicated than it is and [this video shows you how](https://youtu.be/neT7fmZ6sDE) – Maxim Orlov Sep 03 '20 at 12:55
  • 1
    Make sure that `HTTPS=true` is in upper-case, lower-case `https=true` will not work. – Snackoverflow Feb 21 '21 at 08:58
  • A full cmd line for Windows: `set PORT=443&&set HTTPS=true&&set SSL_CRT_FILE=./certs/fullchain.pem&&set SSL_KEY_FILE=./certs/privKey.pem&& npm start` – Farzan May 15 '21 at 20:33
52

You can edit your package.json scripts section to read:

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

or just run set HTTPS=true&&npm start

Just a sidenote, for me, making this change breaks hot reloading for some reason....

-- Note: OS === Windows 10 64-Bit

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
Smitty
  • 1,765
  • 15
  • 22
  • 11
    To get this working without having to type it out every time I also put it in my package.json but I had to omit the `set` and the `&&` for it to work on my iMac. so I just used `HTTPS=true react-scripts start` – Tope May 15 '18 at 00:42
45

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.

wm1sr
  • 1,905
  • 1
  • 16
  • 20
21

Windows (cmd.exe)

set HTTPS=true&&npm start

(Note: the lack of whitespace is intentional.)

Windows (Powershell)

($env:HTTPS = "true") -and (npm start)

Linux, macOS (Bash)

HTTPS=true npm start

Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.

Custom SSL certificate

HTTPS=true SSL_CRT_FILE=<SSLCert.crt> SSL_KEY_FILE=<SSLCert.key> npm start

Linux, macOS (Bash)

HTTPS=true SSL_CRT_FILE=<SSLCert.crt> SSL_KEY_FILE=<SSLCert.key> npm start

To avoid doing it each time: You can include in the npm start script like so:

{
  "start": "HTTPS=true react-scripts start"
}

Or you can create a .env file with HTTPS=true

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • 1
    Custom SSL Certificate in Windows: `set HTTPS=true&&set SSL_CRT_FILE=&&set SSL_KEY_FILE=&&npm start` – Dulanka Feb 24 '21 at 12:14
20

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.

best wishes
  • 5,789
  • 1
  • 34
  • 59
15

set HTTPS=true&&react-scripts start in scripts > start: of package.json as shown below.

"scripts" in package.json:

"scripts": {
    "start": "set HTTPS=true&&react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  • Please don't leave any space in between the commands i.e, HTTPS=true && npm start won't work.

Refer it in official doc. Using HTTPS in Development

(Note: the lack of whitespace is intentional.)

Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
  • 1
    this helped. the script changed slightly so now it would be: "start": "set HTTPS=true&&node scripts/start.js", – user1189352 Jan 22 '20 at 00:28
7

I think it is worth to mention to set PORT=443, default HTTPS standard port. You can avoid to attach :PORT at the end of the address when browsing every time.

su
export HTTPS=true 
export PORT=443
export SSL_CRT_FILE=/PATH/TO/cert.pem     # recommended
export SSL_KEY_FILE=/PATH/TO/privkey.pem  # recommended
npm start

Or

you can put them all in to package.json:

  "scripts": {
    "start": "HTTPS=true PORT=443 react-scripts start",

Then, without exporting:

su
npm start
ghchoi
  • 4,812
  • 4
  • 30
  • 53
7
"scripts": {
"start": "set HTTPS=true&&set PORT=443&&react-scripts start",
........
}

In case you need to change the port and set it to https.

Akhilesh
  • 525
  • 1
  • 9
  • 16
6

Add to file .env (or .env.local) line: HTTPS=true

Klein Maximus
  • 61
  • 1
  • 1
5

Please use this in command prompt

 set HTTPS=true&&npm start
Suneet Patil
  • 608
  • 9
  • 21
4

might need to Install self-signed CA chain on both server and browser. Difference between self-signed CA and self-signed certificate

codeislife
  • 1,446
  • 1
  • 10
  • 7
4

if it's still not working properly because of "your connection is not private" issues (in chrome), this worked for me just fine:

https://github.com/facebook/create-react-app/issues/3441

In short:

  1. First I exported certificate from chrome (view this).
  2. Imported the certificate into Windows (using certmgr.msc).
  3. Allowed chrome://flags/#allow-insecure-localhost flag. How to allow insecure localhost
KurgerBing
  • 173
  • 1
  • 2
  • 16
4

You can create a proxy.HTTPS->HTTP

Create a key and cert.

openssl req -nodes -new -x509 -keyout server.key -out server.cert

Create a file named proxyServer.js

var httpProxy = require('http-proxy');
let fs = require('fs');

httpProxy.createServer({
    target: {
      host: 'localhost',
      port: 3000
    },
    ssl: {
      key: fs.readFileSync('server.key', 'utf8'),
      cert: fs.readFileSync('server.cert', 'utf8')
    }
  }).listen(3000);

From the terminal run

node proxyServer.js

tanner burton
  • 1,049
  • 13
  • 14
3

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.

Dimitri Leite
  • 1,791
  • 13
  • 16
3

Edit your package.json file and change the starting scripts for starting your secures domain. for example https

 {
  "name": "social-login",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-facebook-login": "^4.0.1",
    "react-google-login": "^3.2.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    // update this line "start": "HTTPS=true react-scripts start",
    "start": "set HTTPS=true&&react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

thanks

Rizwan
  • 3,741
  • 2
  • 25
  • 22
2

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.

2

For Windows, try this one

($env:HTTPS = "true") -and (npm start)

I am using VS Code Terminal (powershell).

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
1

I could not get that to work (setting HTTPS=true), instead, i used

react-https-redirect

A simple wrapper around your App component.

Fabiano Tarlao
  • 3,024
  • 33
  • 40
PatrickGoethe
  • 35
  • 1
  • 2
  • 6
1

I am using Windows 10 latest build with Windows Insider Program till this date.

It seems like there are three cases while using Windows 10:

  1. Windows 10 with CMD command line for your NPM
set HTTPS=true&&npm start
  1. Windows 10 with PowerShell command line for your NPM
set HTTPS=true&&npm start
  1. Windows 10 with Linux Bash command line for your NPM ( My Case was this )
HTTPS=true npm start

Documentation: Create react app dev

Anil Prz
  • 1,099
  • 10
  • 14
  • The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine – Zhivko Zhelev Mar 17 '20 at 18:48
1

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.

vaughan
  • 6,982
  • 6
  • 47
  • 63
1

Important point about this issue: if you are looking to use https on LAN (rather than localhost) then SSL certification is an issue because the IP is not static!

This is a nice read on the subject where they explore the option of doing it anyway: SSL For Devices in Local Networks

1

Open the package.json file and change the start script file like given below.

"start": "react-scripts start",

to

"start": "HTTPS=true react-scripts start",

Restart your localhost and check the terminal you are probably able to see the local and on your network runs by HTTPS.

Elavarasan r
  • 1,055
  • 2
  • 12
  • 22
0
HTTPS=true npm start

in the terminal worked for me on Create-React-App

Andy
  • 2,124
  • 1
  • 26
  • 29
  • 2
    I get this error : The term 'HTTPS=true' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. – Newton Sheikh Jul 05 '19 at 08:21
  • 1
    @NewtonSheikh I get the same error, what is the problem ? – TaouBen Aug 25 '19 at 23:46
  • @TaouBen from where are you executing? make sure u use cmd and not powershell ... https://stackoverflow.com/questions/56898943/set-https-in-environmental-variable – Newton Sheikh Aug 28 '19 at 12:53
0

// add this line to android manifest to use http when releasing apk in react native

<application android:usesCleartextTraffic="true">
amit
  • 1