15

I want to run a React project in my Windows (as a virtual machine of my Mac).

In a Command Prompt, after running yarn to install dependencies. I did yarn start. And it gave me 'HTTPS' is not recognized as an internal or external command error.

> yarn start
yarn run v1.13.0
$ HTTPS=true CERT=cert/localhost.crt KEY=cert/localhost.key umi dev
'HTTPS' is not recognized as an internal or external command,
operable program or batch file.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Does anyone know how to fix this?

Edit 1:

I upgraded node to v12.16.1 (yarn to 1.13.0, npm to 6.13.4), and did yarn add https and yarn add https-localhost. However, yarn start still returned 'HTTPS' is not recognized as an internal or external command.

umi dev returned 'umi' is not recognized as an internal or external command, operable program or batch file.:

>umi dev
'umi' is not recognized as an internal or external command, 
operable program or batch file.
CoryCoolguy
  • 1,065
  • 8
  • 18
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • you need https package. Install it locally or globally. – Jai Feb 24 '20 at 10:54
  • Do you mean `SSL Certificate`? – SoftTimur Feb 24 '20 at 10:58
  • no. i mean npm package – Jai Feb 24 '20 at 10:58
  • check this: https://www.npmjs.com/package/https-localhost – Jai Feb 24 '20 at 11:04
  • I think it's because of your node version. From a version later, https became a build-in module on node. What is your node version? – Hamed Navabian Feb 24 '20 at 11:56
  • node version is `10.15.0` – SoftTimur Feb 24 '20 at 12:07
  • I tried `yarn add https-localhost` and added the message to OP. After that, `yarn start` still returned the https error. – SoftTimur Feb 24 '20 at 12:10
  • Please see my OP, it still did not work... – SoftTimur Feb 24 '20 at 14:12
  • 2
    `$ HTTPS=true CERT=cert/localhost.crt KEY=cert/localhost.key umi dev` - That looks like a shell command (Bourne/bash/Unix/Linux/POSIX) that invokes the command `umi dev` after setting variables `HTTPS`, `CERT`, and `KEY`. I'm not familiar with React or Yarn, but could it be it doesn't support Windows, or that something is misconfigured to make it think it's on a Unix-like system? – Keith Thompson Feb 24 '20 at 22:54
  • Make sure you have the following lines in .env file: `HTTPS=true` `PORT=8080` `HOST=localhost` – Maf Apr 20 '20 at 13:55
  • You don't need to install any package neither. – Maf Apr 20 '20 at 14:06
  • Check also my answer about valid certificate: https://stackoverflow.com/questions/41192491/how-can-i-provide-a-ssl-certificate-with-create-react-app/72613085#72613085 – Maf Jun 14 '22 at 07:35

7 Answers7

9

Just add the following lines to .env:

HTTPS=true

PORT=8080

HOST=localhost

If you want to use another host or port, feel free to replace localhost with the IP address you want and the port with any available port number. If you don't have .env file, just create it in your react root folder.

After this you can run yarn start or npm start and your project will use https instead of http.

If you need to use a real certificate inatead of a self-signed, then you can take a look at may answer here: How can I provide a SSL certificate with create-react-app?

Maf
  • 696
  • 1
  • 8
  • 23
  • If you need to use a real certificate inatead of a self-signed, the you can take a look at may answer here: https://stackoverflow.com/questions/41192491/how-can-i-provide-a-ssl-certificate-with-create-react-app/72613085#72613085 – Maf Aug 10 '22 at 16:10
4

I encountered a similar issue running in a Windows environment, the solution was to include '&&' after setting the environment variables and the command that follows.

Example:

set HTTPS=true set CERT=.cert/localhost.crt set KEY=.cert/localhost.key && umi dev

React Example:

set HTTPS=true set SSL_CRT_FILE=.certificates/localhost.crt set SSL_KEY_FILE=.certificates/localhost.key && react-scripts start
Jam
  • 296
  • 2
  • 8
3

On windows you need to include 'set' as in "set HTTPS=true" to set an environment variable.

1

For time being

Instead of: "start": "CERT=cert/localhost.crt KEY=cert/localhost.key umi dev react-scripts start",

Do this only: "start": "react-scripts start",

1

I just had the same problem on Windows but instead of "Command Prompt" I use Git Bash. So if you like Git Bash, this answer might be helpful to you.

  1. Step One: Leave your package.json in its original form, we're going to add the environment variables in the next steps:

    package.json


  1. Step Two: Create a file that will set the environment variables:

    enter image description here


  1. Step Three: Add your environment variables to that file. set didn't work for me but export did:

    enter image description here

Note: The default port is 3000, so if you're not exporting a PORT in this file, React will start at 'https://localhost:3000'


  1. Step Four: source the file. You basically have 2 options:
    • Option 1: In order to use these variables for the current session only:
      enter image description here

    • Option 2: In order to use these variables for any session, add these lines to your ~/.bashrc file:
      enter image description here


  1. Step Five: Run npm start or yarn start and you should be using https on localhost:

    enter image description here

Note: I'm not exporting a PORT here, just using the default port 3000.

Thomas Soos
  • 150
  • 2
  • 11
1

I just remove "start": "HTTPS=true react-scripts start", to "start": "react-scripts start", in file package.json

Anandela
  • 21
  • 3
0

Just by adding HTTPS=true is enough to make it work.

You can add more to the .env file like the PORT if the post is other than 3000.

user3856437
  • 2,071
  • 4
  • 22
  • 27