How to Load environment variables from .env file using Vite?
Last Updated :
22 Aug, 2024
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 configurations for development, production, or testing environments without hardcoding values directly into your code.
Prerequisites
Approach
In Vite, environment variables are loaded from a .env file by defining them with a VITE_ prefix. These variables are then accessible in your React application via import.meta.env. This method allows seamless integration of environment-specific configurations, ensuring that your application is adaptable to different environments.
Steps to load environment variables from .env file using Vite
Step 1: Install Node.js, If you haven’t installed NodeJs, download it from the official website.
Step 2: Create a new Vite Project
npm create vite@latest my-react-app-env --template
Step 3: Select a framework: select the React framework here using the downward arrow key.
Vanilla
Vue
React
Preact
Lit
Svelte
Solid
Qwik
Others
Step 4: Select Variant: choose any variant of your choice using the downward arrow key,i.e: choose JavaScript
TypeScript
TypeScript + SWC
JavaScript
JavaScript + SWC
Step 5: Now, switch to my-react-app-env directory
cd my-react-app-env
Step 6: Install Dependencies
npm install
Dependencies:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
Step 7: Start Server using below command, make sure check your port no on the terminal it may be different for you system.
npm run dev
➜ Local: http://localhost:5173/
Project Structure:
Example: We are using Vite's import.meta.env to load environment variables from the .env file in a React application. In App.jsx, import.meta.env.VITE_API_URL and import.meta.env.VITE_APP_NAME are used to access the VITE_API_URL and VITE_APP_NAME variables, respectively. These values are then displayed in the component, specifying how to integrate environment variables into the React app.
JavaScript
// App.jsx
import React from 'react';
function App() {
// Accessing environment variables
const apiUrl = import.meta.env.VITE_API_URL;
const appName = import.meta.env.VITE_APP_NAME;
return (
<div>
<h1 style={{ color: 'green' }}>GeeksforGeeks</h1>
<h3>Load environment variables from .env file using Vite</h3>
<p>API URL: {apiUrl}</p>
<p>App Name: {appName}</p>
</div>
);
}
export default App;
JavaScript
//.env
VITE_API_URL=https://geeksforgeeks.org/api
VITE_APP_NAME=MyViteApp
Output:
OutputConclusion
By using the VITE prefix in environment variables you can easily load your Vite-powered React applications. This approach simplifies the management of environment-specific configurations, enhancing the flexibility and maintainability of your code.
Similar Reads
How to Use Environment Variables in Vite?
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 v
2 min read
How to Pull Environment Variables with Helm Charts
Helm Chart is widely regarded as the package manager for Kubernetes. While it may appear to be just another package manager, it extends a great deal more. Helm makes managing application components easy by grouping those parts into charts, which can then be easily installed and upgraded.What is Helm
4 min read
C# Program to Get the Environment Variables Using Environment Class
In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information,
3 min read
Reading Environment Variables From Node.js
Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part
2 min read
Read Environment Variables with Python dotenv
Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible softwa
4 min read
Using Environment Variables in Spring Boot's Properties Files
Spring Boot is one of the best frameworks for back-end purposes, and it is mostly used for Web Application development. In this article, we will explain about properties file in Spring Boot using environment variable. The properties file is available in the Resource folder in the Spring Project fold
4 min read
Docker Run with Environment Variables
Containerization has significantly advanced software development by packaging applications and their dependencies together and containerizing them to ensure consistency across multiple environments. Environment variables are one of the most important features of containerization as they make it poss
7 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 Host ReactJS Websites on Vercel with Environment Variables ?
In this article, we will learn how to host our ReactJs projects on Vercel and use Environment Variables in this tutorial. Vercel is a platform for developers to deploy and host their projects online, where they can showcase their skills to others. What is an Environment Variable? We conceal API keys
4 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