<?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: Elis Kaholwe</title>
    <description>The latest articles on DEV Community by Elis Kaholwe (@eliskaholwe).</description>
    <link>https://dev.to/eliskaholwe</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%2F1239268%2Fa6aba06d-a041-4d6f-aec1-c6a123f0282f.jpeg</url>
      <title>DEV Community: Elis Kaholwe</title>
      <link>https://dev.to/eliskaholwe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eliskaholwe"/>
    <language>en</language>
    <item>
      <title>"dotenv" - The Non-Problem Everyone Thinks Is a Problem</title>
      <dc:creator>Elis Kaholwe</dc:creator>
      <pubDate>Thu, 08 Aug 2024 17:23:16 +0000</pubDate>
      <link>https://dev.to/eliskaholwe/dotenv-the-non-problem-everyone-thinks-is-a-problem-2359</link>
      <guid>https://dev.to/eliskaholwe/dotenv-the-non-problem-everyone-thinks-is-a-problem-2359</guid>
      <description>&lt;p&gt;&lt;strong&gt;Who 🤔 needs a middleman when you've got Express built-in?&lt;/strong&gt;&lt;br&gt;
Managing environment variables is a crucial aspect of ensuring your application runs consistently across different environments. Traditionally, developers have relied on the popular "&lt;a href="https://www.npmjs.com/package/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt;" package from npm to load environment variables from a .env file into their Node.js applications. However, with the latest version of &lt;a href="https://www.npmjs.com/package/express" rel="noopener noreferrer"&gt;Express&lt;/a&gt;, there is now a &lt;em&gt;built-in solution that makes environment variable management even simpler.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Environment Variables 🐬
&lt;/h2&gt;

&lt;p&gt;Environment variables are key-value pairs that provide configuration settings for applications. They are typically used to store sensitive information such as API keys, database credentials, and other configuration details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use a .env File?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Security: The primary reason is to protect sensitive information like API keys, database passwords, and secret tokens. By storing these values in a .env file, you prevent them from being accidentally committed to your code repository, reducing the risk of exposure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configuration Management ⚙️: .env files are ideal for storing non-sensitive configuration settings that might vary between different environments (development, staging, production). This includes things like database URLs, API endpoints, and port numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Environment-Specific Values: You can use .env files to manage environment-specific variables that are not suitable for hardcoding in your code. This helps maintain code flexibility and adaptability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Kind of Variables to Store in a .env File?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Sensitive Information 👙:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API keys (e.g., Stripe, Twilio, Google Maps,Rapid API)&lt;/li&gt;
&lt;li&gt;Database credentials (username, password, host, port, database name)&lt;/li&gt;
&lt;li&gt;Secret tokens (e.g., JWT secrets, encryption keys)
OAuth credentials&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Configuration Settings 🛠️:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base URLs for APIs&lt;/li&gt;
&lt;li&gt;File paths&lt;/li&gt;
&lt;li&gt;Port numbers&lt;/li&gt;
&lt;li&gt;Debug flags&lt;/li&gt;
&lt;li&gt;Environment-specific variables (e.g., development, staging, production)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How it used to be
&lt;/h2&gt;

&lt;p&gt;Historically, developers relied on the dotenv package to manage environment variables in Node.js applications. However, with the introduction of the --env-file flag in Express, this dependency is no longer necessary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6b0fn98cgkhu2xwb5xs5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6b0fn98cgkhu2xwb5xs5.jpeg" alt="Image description" width="640" height="790"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/express" rel="noopener noreferrer"&gt;"dotenv"&lt;/a&gt; was a popular package that allowed developers to load environment variables from a .env file into the Node.js process. This approach was convenient but introduced an additional dependency (&lt;em&gt;i know a guy who knows a guy loop&lt;/em&gt;) .&lt;/p&gt;

&lt;h2&gt;
  
  
  How it's going
&lt;/h2&gt;

&lt;p&gt;Modern versions of Express offer a built-in mechanism to load environment variables directly from a .env file. By using the    &lt;code&gt;--env-file&lt;/code&gt; flag when starting the Node.js process, you can bypass the need for dotenv altogether.&lt;br&gt;
&lt;strong&gt;i.e&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;node --env-file .env index.js&lt;/code&gt;&lt;br&gt;
also&lt;br&gt;
&lt;code&gt;nodemon --env-file .env index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command instructs Node.js to load environment variables from the .env file located in the project's root directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Ditching &lt;a href="https://www.npmjs.com/package/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; is a Good Idea
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bcu2f1a0tiryl020re2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bcu2f1a0tiryl020re2.gif" alt="Image description" width="220" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Reduced Dependencies&lt;br&gt;
Eliminating dotenv simplifies project setup and reduces potential conflicts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplified Configuration&lt;br&gt;
The process of loading environment variables becomes more streamlined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Improved Performance&lt;br&gt;
While the performance impact is likely minimal, removing unnecessary dependencies can potentially improve application startup time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; While .env files are helpful, it's essential to implement additional security measures, such as environment variable management tools and secure deployment practices, to protect sensitive information.&lt;/p&gt;

&lt;p&gt;In real life there's a repo,vividly explaining this article,&lt;a href="https://github.com/ElisKaholwe/ditching-dotenv" rel="noopener noreferrer"&gt;github repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>express</category>
      <category>nextjs</category>
      <category>node</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
