<?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"><channel><title><![CDATA[learn with ntsh]]></title><description><![CDATA[learn with ntsh]]></description><link>https://blogs.ntshptl.in</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 13:29:19 GMT</lastBuildDate><atom:link href="https://blogs.ntshptl.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How a Simple dotenv Mistake Turned My Express Server Setup into a Headache (And How You Can Avoid It)]]></title><description><![CDATA[Setting up an Express server is supposed to be straightforward, right? That’s what I thought too. As someone who’s been knee-deep in Docker-based development for a while, I decided to step outside my usual workflow to try out Google Social Login API ...]]></description><link>https://blogs.ntshptl.in/how-a-simple-dotenv-mistake-turned-my-express-server-setup-into-a-headache-and-how-you-can-avoid-it</link><guid isPermaLink="true">https://blogs.ntshptl.in/how-a-simple-dotenv-mistake-turned-my-express-server-setup-into-a-headache-and-how-you-can-avoid-it</guid><category><![CDATA[Environment variables]]></category><category><![CDATA[.env]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[dotenv]]></category><dc:creator><![CDATA[Nitish Patel]]></dc:creator><pubDate>Thu, 02 Jan 2025 01:56:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735783303294/8804c581-4087-4cb6-8c95-f32b2b6c6f87.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><img src="https://miro.medium.com/v2/resize:fit:700/0*mOUB9eNfT19RktPy" alt="Generated By Open AI" class="image--center mx-auto" /></p>
<p>Setting up an Express server is supposed to be straightforward, right? That’s what I thought too. As someone who’s been knee-deep in Docker-based development for a while, I decided to step outside my usual workflow to try out Google Social Login API on an Express server — without Docker this time.</p>
<p>I thought, “Why bother with Docker for a quick trial?” So, I went ahead and set up my project the old-fashioned way. I created a <code>.env</code> file and installed the <code>dotenv</code> package to manage environment variables. Everything seemed to be going smoothly.</p>
<p>Here’s what my <code>app.ts</code> looked like:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> express, { Express } <span class="hljs-keyword">from</span> <span class="hljs-string">"express"</span>;
<span class="hljs-keyword">import</span> cors <span class="hljs-keyword">from</span> <span class="hljs-string">"cors"</span>;
<span class="hljs-keyword">import</span> connectDb <span class="hljs-keyword">from</span> <span class="hljs-string">"./services/connectDB"</span>;
<span class="hljs-keyword">import</span> passport <span class="hljs-keyword">from</span> <span class="hljs-string">"passport"</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">"./routes"</span>;
<span class="hljs-keyword">import</span> bodyParser <span class="hljs-keyword">from</span> <span class="hljs-string">"body-parser"</span>;
<span class="hljs-keyword">import</span> session <span class="hljs-keyword">from</span> <span class="hljs-string">"express-session"</span>;
<span class="hljs-keyword">const</span> app: Express = express();
<span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">"dotenv"</span>;
dotenv.config();
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'MONGO_URI in app.ts'</span>, process.env.MONGODB_URI);
</code></pre>
<p>My <code>.env</code> file was set up like this:</p>
<pre><code class="lang-plaintext">MONGODB_URI=mongodb://localhost:27017
GOOGLE_CLIENT_SECRET=VERY_VERY_SECRET
GOOGLE_CLIENT_ID=VERY_SECRET
</code></pre>
<p>When I ran the server, the <code>MONGODB_URI</code> printed out just fine in <code>app.ts</code>. So far, so good, right?</p>
<p>Then, I moved on to my database connection logic in <code>services/connectDB.ts</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> mongoose <span class="hljs-keyword">from</span> <span class="hljs-string">"mongoose"</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"MONGODB_URI: "</span>, process.env.MONGODB_URI);
<span class="hljs-keyword">const</span> uri = process.env.MONGODB_URI || <span class="hljs-string">`mongodb://localhost:27017/test`</span>;
<span class="hljs-keyword">const</span> connectDb = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Connecting String: "</span>, uri);
        <span class="hljs-keyword">await</span> mongoose.connect(uri); <span class="hljs-comment">// Connect to MongoDB</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Connected to the database"</span>);
    } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-built_in">console</span>.error(error);
    }
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connectDb;
</code></pre>
<p>To my surprise, the <code>MONGODB_URI</code> was <code>undefined</code> here. Confused, I added the <code>dotenv</code> import to this file, and suddenly everything worked as expected. But why was this happening? Why did I have to import <code>dotenv</code> in every file where I wanted to access my environment variables?</p>
<p>I went on a hunt through countless blogs, tried different approaches, but nothing clicked. Frustrated, I turned to ChatGPT, thinking, “Why not? Let’s see what it suggests.”</p>
<p><em>The solution?</em></p>
<p><img src="https://miro.medium.com/v2/resize:fit:480/0*t5YMVsR1LSdMf-Tq.gif" alt class="image--center mx-auto" /></p>
<p>A simple but critical change: import <code>dotenv</code> at the top of your main file, before anything else. Just like that, everything started working perfectly.</p>
<p>Here’s the updated <code>app.ts</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> express, { Express } <span class="hljs-keyword">from</span> <span class="hljs-string">"express"</span>;
<span class="hljs-comment">// Updated CODE</span>
<span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">"dotenv"</span>;
dotenv.config();
<span class="hljs-keyword">import</span> cors <span class="hljs-keyword">from</span> <span class="hljs-string">"cors"</span>;
<span class="hljs-keyword">import</span> connectDb <span class="hljs-keyword">from</span> <span class="hljs-string">"./services/connectDB"</span>;
<span class="hljs-keyword">import</span> passport <span class="hljs-keyword">from</span> <span class="hljs-string">"passport"</span>;
<span class="hljs-keyword">import</span> routes <span class="hljs-keyword">from</span> <span class="hljs-string">"./routes"</span>;
</code></pre>
<p>So, if you’re reading this because you’re tearing your hair out over <code>dotenv</code> not working in your Express app — stop searching and just move that <code>dotenv</code> import to the top of your main file.</p>
<p><strong><em>Problem solved.</em></strong></p>
]]></content:encoded></item></channel></rss>