Getting Env variables to work with import.meta.env
#9000
-
I am trying to get some environment variables to work but for the life of me I can not...I have looked through a few SO answers and they all seem to point to how I have set things up. Yet I keep getting undefined. This is how I am testing the variable. import {useState} from 'react'
import {FaCog} from 'react-icons/fa'
function App() {
const [data, setData] = useState([])
const [dealId, setDealId] = useState('')
const [loading, setLoading] = useState(null)
const [error, setError] = useState('')
// Counter
const [count, setCounter] = useState(0)
console.log('HOST:', import.meta.env.HOST) // returns undefined This is my # .env
HOST=127.0.0.1:8000 This is my structure (removed node_modules) for ease
Where am I going wrong? I have restated the vite server many times already...i am stumped |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 34 replies
-
Beta Was this translation helpful? Give feedback.
-
I made env vars e.g. |
Beta Was this translation helpful? Give feedback.
-
After a bit of head-bashing it seems for me what has really worked is to place the
|
Beta Was this translation helpful? Give feedback.
-
why |
Beta Was this translation helpful? Give feedback.
-
In my case the issue was with optional chaining. Not sure why it does not work when optional chaining is added. .env file placed in root folder + "SPG" added as envPrefix
getting env variables
|
Beta Was this translation helpful? Give feedback.
-
Use a base variable E.g const api_key = import.env.VITE_API KEY |
Beta Was this translation helpful? Give feedback.
-
I'm getting I added a However, I cannot access any variables. I get undefined for everything. So I tried to log just Here is what I tried:
What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
-
FOUND THE SOLUTION: Looks like vite has changed how you can access environment inside the config file. -> import.meta.env works once the app is live, not before that. This will work if you print it in a react/any js component. To access env variable inside vite.config.js here is how yo do it:
You can access the environment variable as a parameter, in the above example, the command and mode variables are extracted from env. Cheers! |
Beta Was this translation helpful? Give feedback.
-
Please help !! I still cannot access the variables. The .env file is in the root and with preflix. I tried VITE_ and REACT_APP. VITE 5.1 What is wrong???? I can not understand. It is a brand new installation of react + vite. |
Beta Was this translation helpful? Give feedback.
-
OK, I seem to be having similar issues getting environment variables to work. In particular, I can get them to work in 'development' mode, but not in 'production' mode (vite preview).
As I said, both methods work when I run as development build. But neither gives me the env variable when I use production. Please, what am I doing wrong?! |
Beta Was this translation helpful? Give feedback.
You need to prefix the env name with
VITE_
.https://vitejs.dev/guide/env-and-mode.html#env-files:~:text=To%20prevent%20accidentally%20leaking%20env%20variables%20to%20the%20client%2C%20only%20variables%20prefixed%20with%20VITE_%20are%20exposed%20to%20your%20Vite%2Dprocessed%20code.%20e.g.%20the%20following%20file%3A