How to Use Environment Variables in Vite?
Last Updated : 20 Aug, 2024
In order to create your Vite application in different environments such as development, staging and production, you need to use environment variables. They enable you to manage confidential data like API keys or service URLs without having to code them inside your source code. By using environment variables, it is easy to change configurations depending on the environment in which the application runs; making sure that they would encounter no errors or security problems. This article will help you understand how to set up and use environment variables with Vite, including some common use cases and best practices.
These are the following approaches:
Steps to Create Vite Application
Step 1: Ensure NodeJs is installed in your machine, if not download it from the official website.
Step 2: Create a new Vite Project by using the following command:
npm create vite@latest vite-project --template
Step 3: Select the framework, choose React using the downward arrow key.
Vanilla
Vue
React
Preact
Lit
Svelte
Solid
Qwik
Others
Step 4: Select Variant, choose any one of the below variants of your choice by using the downward arrow key, here we choose JavaScript variant.
TypeScript
TypeScript + SWC
JavaScript
JavaScript + SWC
Step 5: Now, switch to vite-project directory
cd vite-project
Step 6: Install Dependencies
npm install
Package.json:
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.2.0"
}
Project Structure:
Project structure
Step 7: Start the server by using the following command
npm run dev
Now, the Vite app is running on your local host.
➜ Local: http://localhost:5173/
Approach 1: Using '.env' Files
- Vite allows loading of environment variables from .env files. These are files which contain values that you want to be available everywhere in the application.
- Make a file named .env at project root and then define variables in following syntax: VITE_SOME_KEY = some_value. The only variables exposed to the client are those that start with VITE_.
Syntax:
VITE_KEY_NAME = value
Example:
VITE_API_URL = https://api.example.com
VITE_APP_NAME = MyApp
.envApproach 2: Directly in the Vite Configuration FIle
- The Vite Configuration file (vite.config.js) supports direct usage of the environmental variables. This method helps to define variable specific to configuration.
- The Vite Configuration file accesses environmental variables using process.env.
Syntax:
//vite.config.js:
import { defineConfig } from 'vite';
export default defineConfig({
define: {
__VARIABLE_NAME__: JSON.stringify(process.env.VARIABLE_NAME)
}
});
Approach 3: Accessing Environment Variables in your Code
After defining environment variables and adding a prefix of VITE_, you would be able to access them right from within your code by using import.meta.env.
Syntax:
import.meta.env.VITE_KEY_NAME
Example: You can add this code to any of your file to get the respective output.
JavaScript console.log(import.meta.env.VITE_API_URL);
console output Similar Reads
How To Set Environment Variables In Jenkins ?
Jenkins is a widely used open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) processes. One powerful feature of Jenkins is its capability to configure environment variables, which are important for various functions like defining parameters, paths, a
6 min read
How to Load environment variables from .env file using Vite?
The environment variables in the application are managed through .env files in a Vite project, allowing you to configure and access various settings dynamically. By prefixing variables with VITE_, Vite exposes them to your applicationâs runtime environment. This approach facilitates different config
2 min read
Set environment variable in MATLAB
Setting environment variables with the help of setenv() function. The setenv() function is used to set the specified value of an operating system environment variable. Here the environment variable "name" will be replaced with "value", where "name" and "value" is its parameters. Syntaxsetenv(name, v
1 min read
How to Read Environment Variables in Scala?
Scala stands for scalable language. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically typed language although unlike other statically
3 min read
How to add environment variable in MacOS?
While programmers work on different Programming Languages, they use to declare Variables to work on any certain piece of code. The Variables in general terms are used to store some values that can be again accessed in the future. There are some Operating System Variables present that are known as En
3 min read
How To Configure And Use Environment Variables in NestJS?
Environment variables are an important part of application development, allowing developers to configure applications in different environments (development, staging, production) without hardcoding sensitive or environment-specific information into the application code. In this article, we'll walk t
2 min read
How to Set Environment Variable in AWS Lambda ?
AWS Lambda is a managed serverless deployment service of AWS that can be used for the implementation of various applications in the cloud. AWS lambda may use various environment variables for the execution of different operations. eg. Database Credentials, API Keys .etc. There are various ways we ca
4 min read
Environment Variables in Java
In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
5 min read
Next.js Environment Variables
Environment variables are a fundamental aspect of modern web development, allowing developers to configure applications based on the environment they are running in (development, testing, production, etc.). In Next.js, environment variables provide a flexible and secure way to manage configuration s
3 min read
Next.js Environment Variables
In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any o
2 min read