Configuring HTTPS in Vite Local Development Environment: A Comprehensive Guide Using @vitejs/plugin-basic-ssl

Dec 07, 2025 · Programming · 12 views · 7.8

Keywords: Vite | HTTPS | local development

Abstract: This article explores solutions for configuring HTTPS in Vite's local development environment, focusing on the officially recommended @vitejs/plugin-basic-ssl plugin. It details the installation and configuration steps, analyzes its working principles and applicable scenarios. As supplements, it briefly introduces alternative approaches such as vite-plugin-mkcert and manual setup using mkcert tool, helping developers choose suitable methods based on specific needs. By comparing different solutions, the article emphasizes the importance of using HTTPS in development environments and reminds readers of certificate management differences between development and production.

Introduction

In modern web development, HTTPS has become a standard configuration, providing not only data transmission security but also ensuring browser functionality integrity, such as access to Service Workers and certain APIs. Enabling HTTPS in local development environments simulates production settings, avoiding issues caused by protocol differences. Vite, as a next-generation frontend build tool, offers multiple ways to support local HTTPS. This article focuses on the officially recommended @vitejs/plugin-basic-ssl plugin and discusses other optional solutions.

Using the @vitejs/plugin-basic-ssl Plugin

@vitejs/plugin-basic-ssl is an official Vite plugin designed to quickly enable HTTPS in development environments. Its core advantage lies in simplifying certificate generation and management, eliminating the need for manual handling of certificate files. Installing the plugin is straightforward via npm or yarn: npm install -D @vitejs/plugin-basic-ssl. After installation, configure it in the vite.config.js or vite.config.ts file. Here is a basic example:

import { defineConfig } from 'vite';
import basicSsl from '@vitejs/plugin-basic-ssl';

export default defineConfig({
  plugins: [
    basicSsl()
  ]
});

Once configured, when running the development server, the plugin automatically generates a self-signed certificate and enables HTTPS. In Vite 5 and above, no additional server: { https: true } setting is needed, as the plugin handles it automatically. For Vite 4 or earlier versions, explicit configuration of the server.https option may be required. When accessing the local server, browsers may display certificate warnings because self-signed certificates are not trusted by the system. Developers can manually trust the certificate or use the browser's "allow insecure localhost" option as a temporary workaround.

How the Plugin Works and Considerations

The @vitejs/plugin-basic-ssl plugin uses Node.js's crypto module to dynamically generate certificates in the background. These certificates are self-signed, meaning they are not validated by a public Certificate Authority (CA), so browsers mark them as "not secure." In development environments, this is generally acceptable, as the focus is on simulating HTTPS protocol behavior rather than genuine security validation. However, developers must note that this plugin is only for development environments and should never be used in production. Production environments require certificates issued by trusted CAs, such as those obtained via Let's Encrypt, or handled by reverse proxies like Nginx.

Additionally, certificates generated by the plugin are temporary and may be regenerated each time the development server starts, which can cause browser caching issues. If persistent certificate errors occur, try clearing the browser cache or restarting the development server. For team collaboration projects, it is advisable to document the HTTPS configuration to ensure all members use a consistent environment.

Alternative Solutions: vite-plugin-mkcert and Manual Configuration

Besides @vitejs/plugin-basic-ssl, the community offers other solutions. vite-plugin-mkcert is a popular third-party plugin based on the mkcert tool, which can generate system-trusted certificates. After installation, configure it as follows: import mkcert from 'vite-plugin-mkcert'; export default defineConfig({ plugins: [ mkcert() ] });. On first run, the system may prompt for a password to install the root certificate, after which browsers will no longer show warnings. This method provides a better user experience but relies on external tools and system permissions.

Another approach is to manually generate certificate files using the mkcert tool and reference them in the Vite configuration. For example, generate certificates via command line: mkcert -key-file key.pem -cert-file cert.pem localhost, then configure in vite.config.js: server: { https: { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') } }. This method is more flexible but requires additional steps to manage certificate files, suitable for development scenarios with higher security requirements.

Conclusion and Best Practices

When configuring HTTPS in Vite's local development environment, the @vitejs/plugin-basic-ssl plugin is the preferred solution due to its official support and ease of use. It simplifies certificate management and suits most development scenarios. Developers should always remember that this is only a development tool, and production environments require proper certificates. For projects needing a more seamless experience, consider vite-plugin-mkcert; for advanced users, manual configuration offers more control. Regardless of the method chosen, enabling local HTTPS improves development quality and ensures application consistency across different environments. It is recommended to configure HTTPS early in the project to avoid migration issues later.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.