DEV Community

Cover image for Run React App Localhost in Https in 2 steps
Krushna Chandra Dash
Krushna Chandra Dash

Posted on

Run React App Localhost in Https in 2 steps

Question->
How do I use the React development server to serve requests from https while working on localhost?

Ex ->https://localhost:3000

Advantages of using Httpsin localhost

1. Access to 3rd party API's without CORS error.
2. Working with cookies made easy.
3. Accessing Google and Facebook API's without deploying the 
    application.
Enter fullscreen mode Exit fullscreen mode

For React App Created with Vite


Step-1

1.Install vite-plugin-mkcert package.

Step-2

  1. Change in vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import mkcert from 'vite-plugin-mkcert'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(),mkcert()],
  server:{
    port:3000,
    https: true
  }

})
Enter fullscreen mode Exit fullscreen mode

and then start your application with npm run dev


For React App Created with create-react-app


Step-1

1.Install cross-env package.

Step-2

  1. Modify start script in package.json and it should look like this
    "start": "cross-env HTTPS=true react-scripts start",

Enter fullscreen mode Exit fullscreen mode

and Done .. Now Run your app using npm start.



if you get any package issue plese install all the package again using npm install or yarn install

If facing any issue in browser like this
Run React App Localhost in Https in 2 steps

then click on advancebutton and click on continue to site.

Run React App Localhost in Https in 2 steps

.. Happy coding..

if this article helps please don't forget to Share with other..

Top comments (0)