Logo

dev-resources.site

for different kinds of informations.

Run your Sveltekit (or any vite) localhost server with HTTPS

Published at
8/27/2024
Categories
sveltekit
tutorial
vite
localhost
Author
digitaldrreamer
Categories
4 categories in total
sveltekit
open
tutorial
open
vite
open
localhost
open
Author
15 person written this
digitaldrreamer
open
Run your Sveltekit (or any vite) localhost server with HTTPS

A simple no-nonsense guide to running your vite-powered localhost server with HTTPS.

Step one - Install [MkCert]

(https://github.com/FiloSottile/mkcert)
MkCert is a simple zero-config tool to make locally trusted development certificates with any names you'd like.

On Windows:

  • Using Chocolatey:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Enter fullscreen mode Exit fullscreen mode

If you're using powershell, run this in addition:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode

Run choco in your terminal to confirm the installation. You should see something like this:

Choco Version Check
Run this in your terminal to scaffold a new sveltekit project

On Mac:
With Homebrew:

brew install mkcert
brew install nss # if you use Firefox
Enter fullscreen mode Exit fullscreen mode

With MacPorts

sudo port selfupdate
sudo port install mkcert
sudo port install nss # if you use Firefox
Enter fullscreen mode Exit fullscreen mode

On Linux
First install certutil:

sudo apt install libnss3-tools
    -or-
sudo yum install nss-tools
    -or-
sudo pacman -S nss
    -or-
sudo zypper install mozilla-nss-tools
Enter fullscreen mode Exit fullscreen mode

Then install Homebrew on Linux:

test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Install mkcert using Homebrew on Linux

brew install mkcert
Enter fullscreen mode Exit fullscreen mode

For Arch Linux users, mkcert is available on the official Arch Linux repository:

sudo pacman -Syu mkcert
Enter fullscreen mode Exit fullscreen mode

Step two - Install Local Cerificate Authority

Run mkcert -install. You should see something like this.

> mkcert -install
Created a new local CA 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊
Enter fullscreen mode Exit fullscreen mode

Step 3 - Create your certificate with mkcert:
You can move to your project directory and remove 'your_project' from the commands below:

mkdir your_project/cert
cd your_project/cert
mkcert -key-file key.pem -cert-file cert.pem localhost
Enter fullscreen mode Exit fullscreen mode

Step 4 - Update your vite config:

Include your cert files into your vite config:

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import fs from 'fs';

export default defineConfig({
    plugins: [sveltekit()],
// Added Config
    server: {
        https: {
            key: fs.readFileSync(`${__dirname}/../cert/key.pem`),
            cert: fs.readFileSync(`${__dirname}/../cert/cert.pem`)
        },
        proxy: {}
    }
// Added Config End
});
Enter fullscreen mode Exit fullscreen mode

Step 5 - Run dev server
Run npm run dev or pnpn run dev or whatever you deem fit and voila:

Secure Connection

Secure Connection

Happy Hacking!

PS: Relevant docs
PS2: How to change your sveltekit (or any vite-powered) dev server port.

localhost Article's
30 articles in total
Favicon
Accessing a Laptop's Server on Port 80 from Your Phone via Wireless Debugging
Favicon
Expose Your Localhost to a Public URL
Favicon
Comprehensive Guide to Localhost IP Addresses: 127.0.0.1
Favicon
Exploring Localhost 127.0.0.1:62893: Setup and Usage Guide
Favicon
Exploring Localhost 127.0.0.1:49342: Setup and Usage Guide
Favicon
127.0.0.1:57573: Benefits, Comparisons, and Troubleshooting Guide
Favicon
Understanding Localhost Networking: A Comprehensive Guide
Favicon
Access Localhost on Mobile Within the Same Network
Favicon
Set Up Your Own Local GitLab Server | Self-hosted GitLab
Favicon
Run your Sveltekit (or any vite) localhost server with HTTPS
Favicon
Change your Sveltekit (or any vite-powered) dev server localhost port
Favicon
How to Install or Download MongoDB 7 on Windows 10/11 | Step by Step Guide for Localhost
Favicon
What is Localhost in Development Mode?
Favicon
How to share your "localhost" with the world
Favicon
Trabalhando com AWS em um ambiente localhost
Favicon
Best DX for cookies on localhost
Favicon
How to access your site in mobile phone during development
Favicon
Fix MySQL Error: Access Denied for User ‘root’@’localhost’
Favicon
Setting up an Alias for a Directory in Apache2 Server
Favicon
How can I set up SSL on localhost (https://localhost/)?
Favicon
How to test https on localhost with help of Caddy
Favicon
How To Use Visual Studio Dev Tunnels to Receive Webhooks on Localhost
Favicon
How to Preview localhost Website on Mobile Phone
Favicon
How to stop Localhost server running on multiple ports.
Favicon
Let's Run PHP On Localhost with XAMPP Server
Favicon
How to Fix the XAMPP Error “MySQL Shutdown Unexpectedly”
Favicon
Nuxt and Cloudflare, how to get SSR working (2 settings)
Favicon
How to use LocalXpose with a Localhost WordPress
Favicon
What Happens When You Visit http://localhost - A Closer Look
Favicon
Share your localhost with Cloudflare tunnels

Featured ones: