<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: KOUSHIK</title>
    <description>The latest articles on DEV Community by KOUSHIK (@kaushik_9_f298f9e75daa56e).</description>
    <link>https://dev.to/kaushik_9_f298f9e75daa56e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2911784%2F6f4b878e-b4ca-47ca-b239-b7bfb87864bc.png</url>
      <title>DEV Community: KOUSHIK</title>
      <link>https://dev.to/kaushik_9_f298f9e75daa56e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaushik_9_f298f9e75daa56e"/>
    <language>en</language>
    <item>
      <title>Why is My Vite Env Variable undefined? Here's the Simple Fix.</title>
      <dc:creator>KOUSHIK</dc:creator>
      <pubDate>Thu, 08 May 2025 11:50:44 +0000</pubDate>
      <link>https://dev.to/kaushik_9_f298f9e75daa56e/why-is-my-vite-env-variable-undefined-heres-the-simple-fix-6no</link>
      <guid>https://dev.to/kaushik_9_f298f9e75daa56e/why-is-my-vite-env-variable-undefined-heres-the-simple-fix-6no</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
You’re building a React app with Vite and decide to store sensitive keys or configuration values in a .env file. You set everything up:&lt;/p&gt;

&lt;p&gt;VITE_API_KEY="123456"&lt;br&gt;
And then try to log it in your app:&lt;/p&gt;

&lt;p&gt;console.log(import.meta.env.VITE_API_KEY);&lt;br&gt;
But what you get is… undefined.&lt;br&gt;
What gives?&lt;/p&gt;

&lt;p&gt;The Common Mistake: .env File in the Wrong Directory&lt;br&gt;
The issue usually isn’t Vite’s config, nor is it your JavaScript. The most common culprit is file placement.&lt;/p&gt;

&lt;p&gt;Your .env file must be in the root directory of your Vite project — the same level as vite.config.ts or package.json.&lt;/p&gt;

&lt;p&gt;✅ Correct:&lt;/p&gt;

&lt;p&gt;VITE_STREAM_KEY="abc123"&lt;br&gt;
❌ Incorrect:&lt;/p&gt;

&lt;p&gt;STREAM_KEY="abc123"  // Will be undefined . Add "VITE_" at prefix&lt;br&gt;
❌ Wrong:&lt;/p&gt;

&lt;p&gt;my-app/&lt;br&gt;
└── src/&lt;br&gt;
    └── .env&lt;br&gt;
my-app/&lt;br&gt;
├── .env&lt;br&gt;
├── vite.config.ts&lt;br&gt;
├── package.json&lt;br&gt;
└── src/&lt;br&gt;
How Vite Handles Env Variables&lt;br&gt;
Vite only exposes variables that start with the VITE_ prefix to your client-side code. If your variable doesn't use this prefix, it won't show up at all.&lt;/p&gt;

&lt;p&gt;✅ Correct:&lt;/p&gt;

&lt;p&gt;VITE_STREAM_KEY="abc123"&lt;br&gt;
❌ Incorrect:&lt;/p&gt;

&lt;p&gt;STREAM_KEY="abc123"  // Will be undefined . Add "VITE_" at prefix&lt;br&gt;
To use the value:&lt;/p&gt;

&lt;p&gt;console.log(import.meta.env.VITE_STREAM_KEY);&lt;br&gt;
Bonus: Reloading the Dev Server&lt;br&gt;
Any time you modify the .env file, you must restart the Vite dev server. Otherwise, changes won’t be picked up.&lt;/p&gt;

&lt;p&gt;npm run dev&lt;/p&gt;

&lt;h1&gt;
  
  
  or
&lt;/h1&gt;

&lt;p&gt;yarn dev&lt;br&gt;
Final Debug Tip&lt;br&gt;
To verify all available env variables:&lt;/p&gt;

&lt;p&gt;console.log(import.meta.env);&lt;br&gt;
This will show the full list of available Vite-exposed variables at runtime.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
If you’re getting undefined from your Vite env variable:&lt;/p&gt;

&lt;p&gt;✅ Make sure it starts with VITE_&lt;br&gt;
✅ Place your .env file at the project root&lt;br&gt;
✅ Restart the dev server&lt;br&gt;
A tiny mistake — but it can cost you hours. Hopefully, this post saved you from that!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
