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
3 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
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
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
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
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 import variables from another file in Python?
To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your cur
4 min read