<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harry Sotnick-Hommel on Medium]]></title>
        <description><![CDATA[Stories by Harry Sotnick-Hommel on Medium]]></description>
        <link>https://medium.com/@hmsh2000?source=rss-af41ee1cff0a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Tn-SOvF3xTQeFBNk</url>
            <title>Stories by Harry Sotnick-Hommel on Medium</title>
            <link>https://medium.com/@hmsh2000?source=rss-af41ee1cff0a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 22 May 2026 13:15:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hmsh2000/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[The Hidden Complexity of .env in Vite React Projects]]></title>
            <link>https://medium.com/@hmsh2000/the-hidden-complexity-of-env-in-vite-react-projects-67b6a481437b?source=rss-af41ee1cff0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/67b6a481437b</guid>
            <category><![CDATA[vitejs]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Harry Sotnick-Hommel]]></dc:creator>
            <pubDate>Tue, 21 Jan 2025 17:20:44 GMT</pubDate>
            <atom:updated>2025-01-21T17:35:08.464Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Hidden Complexity of Environment Variables in Vite React Projects</h3><p>As a developer stepping into the React ecosystem, one of the first challenges you’ll face is protecting your API keys when pushing code to GitHub. While the solution seems straightforward — just use .env (environment variables), right? — there’s a subtle detail in Vite’s implementation that can leave you scratching your head for hours.</p><h3>The Scenario</h3><p>Picture this: You’re building your first React project, perhaps a movie database app using TMDB’s API. You’ve done everything by the book:</p><ol><li>Created a `.env` file</li><li>Added it to `.gitignore`</li><li>In .env: Placed your api key string within <br>e.g. VITE_API_KEY = “[API STRING HERE]”</li><li>Accessed it on the page you need it using `import.meta.env.VITE_API_KEY`</li></ol><p>Yet, you keep getting that frustrating error:</p><pre>GET https:/[API_WEB_LINK]?api_key=undefined 401 (Unauthorized)<br></pre><h3>The Root of the Problem</h3><p>The issue isn’t in your code, but knowledge of project structure. Here’s the key insight: <strong>Vite requires your `.env` file to be in the same directory where you run your development server</strong>.</p><p>Consider this common project structure:<br>my-project/<br><strong>├── .env<br>├── .gitignore<br>└── frontend/<br> ├── src/<br> ├── package.json<br> └── vite.config.js</strong></p><p>If you’re running `npm run dev` from within the `frontend` directory (as recommended), Vite won’t be able to find the `.env` file in the parent directory. This is a deliberate design choice by Vite for security and clarity.</p><h3>The Solution</h3><p>Instead of restructuring your project, you can tell Vite exactly where to look for your .env file by configuring the envDir option in your vite.config.js:</p><pre>import { defineConfig } from “vite”;<br>import react from “@vitejs/plugin-react”;<br>import { resolve } from “path”;<br><br>export default defineConfig({<br> plugins: [react()],<br> envDir: resolve(__dirname, &quot;..&quot;), // Tell Vite to look for .env one directory above<br>});</pre><p>This elegant solution allows you to:</p><ul><li>Keep your .env file at the root level</li><li>Maintain a clean project structure</li><li>Access environment variables correctly in your React application</li></ul><h3>Why This Matters</h3><p>This configuration highlights an important aspect of modern web development: the flexibility of build tools. While Vite has its defaults, it also provides ways to customize its behaviour to match your project’s needs. Understanding these configuration options can save you hours of debugging and prevent the need to compromise on your preferred project structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K5_eLtLHmIIP7zM3VaP9dg.png" /></figure><h3>Best Practices Moving Forward</h3><p>When setting up a new Vite React project:</p><ul><li>Decide on your preferred location for .env files early in development</li><li>Configure vite.config.js accordingly using the envDir option</li><li>Always prefix your Vite environment variables with VITE_</li><li>Verify your .gitignore includes .env</li><li>Remember that import.meta.env only works in Vite projects</li></ul><p>By keeping these points in mind, you’ll save yourself from spending hours debugging what appears to be a simple configuration issue.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67b6a481437b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>