<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Vanilla - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Vanilla - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 26 May 2026 04:44:26 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/vanilla/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Environment Variables in VanillaJS ]]>
                </title>
                <description>
                    <![CDATA[ By Caleb Olojo In this article, you will learn about environment variables in Vanilla JavaScript. You'll also learn how to serve API keys to your application through the Netlify build command. What are JavaScript environment variables? Environment va... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-environment-variables-in-vanillajs/</link>
                <guid isPermaLink="false">66d45de04a7504b7409c334d</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Vanilla ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 02 Jun 2021 22:54:18 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/05/nguy-n-phuc-6ZO3rE6OLew-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Caleb Olojo</p>
<p>In this article, you will learn about environment variables in Vanilla JavaScript. You'll also learn how to serve API keys to your application through the Netlify build command.</p>
<h2 id="heading-what-are-javascript-environment-variables">What are JavaScript environment variables?</h2>
<p>Environment variables are very common when you're using JavaScript frameworks like React or Vue for creating frontend user interfaces or NodeJS on the server side. </p>
<p>The whole point (or at least, the way I understand it) of environment variables is that they give you the flexibility to set conditions for how you want the application or software to behave in different modes – development and production.</p>
<p>You create these conditions when the UI/frontend of the software gets to interact with an API or a backend server that requires a method of authentication before providing the results of the action (like an API call). The most common method involves provisioning an API key before you can complete a request.</p>
<p>If you've tried getting data from an API before, you have to supply this API key so that the request for data can be successful. This involves adding an <code>Authorization</code> header to the API call.</p>
<p>Take a look at a typical fetch request and its authorization header below.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> apiCall = <span class="hljs-function">() =&gt;</span> {
    fetch(url, {
        <span class="hljs-attr">headers</span>: {
            <span class="hljs-attr">Authorization</span>: <span class="hljs-string">`bearer <span class="hljs-subst">${private_api_key}</span>`</span>
        }
    })
    .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
    .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
    .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">JSON</span>.stingify(err))
}
</code></pre>
<p>Environment variables store variables, as the name implies. The values or things that get assigned to these variables could be API keys that you need to perform certain requests or operations. </p>
<p>To create an environment variable, all you need to do is create a new file called .env in the root folder of the project you're working on. Then you can begin to add all the variables that you do not want to reveal to anyone. </p>
<p>The <code>.gitignore</code> file holds the list of files that Git shouldn't track, and the <code>.env</code> file will be in this file.</p>
<h2 id="heading-how-to-use-env-files-in-vanillajs">How to Use .env Files in VanillaJS</h2>
<p>You use environment variables in the back-end of an application. Now, you're probably like "but I can create a <code>.env</code> file in a React app". </p>
<p>The truth is, you're quite right – but React has been bootstrapped in such a way that Node.js is included in it. This means that you need to use the Node package manager to perform certain operations.</p>
<p>You can also create a .env file when you're using VanillaJS, but you wouldn't be able to access the process.env global variable that Node provides at runtime. Node treats the <code>.env</code> file as an object, so it has the ability to do this: <code>process.env.env_variable</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> env = {
    <span class="hljs-attr">env_variable</span>: <span class="hljs-string">"bgrtyaqQtyfadV0F08BHGvfsgskl"</span>,
    <span class="hljs-attr">topic_id</span>: <span class="hljs-string">"F08BHGvfsgsklgrtyaqQtyfadV0F08"</span>
}

<span class="hljs-built_in">console</span>.log(process.env.env_variable)

<span class="hljs-comment">// prints bgrtyaqQtyfadV0F08BHGvfsgskl to the console</span>
</code></pre>
<p>You use VanillaJS on the client-side, so it's not really feasible to create a <code>.env</code> and use environment variables. This is because you can't use the process.env global variable Node provides (to get access to the variables created inside the <code>.env</code> file) in the browser. </p>
<p>So how then can you actually use environment variables? Hmm...especially since you can't use environment variables while writing client-side JavaScript (I mean VanillaJS). </p>
<p>The npm package called <a target="_blank" href="https://npmjs.org/dotenv">dotenv</a> provides one solution because it has an access to the Node global variable <code>process.env</code>.</p>
<p>Once you have the package installed, a <code>node_modules</code> it'll automatically create a folder accompanied with two files, <code>package.json</code> and <code>package-lock.json</code>. These hold the details of the application. </p>
<p>But as soon as you use it, JavaScript will throw an error saying that <code>require</code> is not defined:</p>
<pre><code class="lang-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">"dotenv"</span>).config()

<span class="hljs-keyword">const</span> apiCall = <span class="hljs-function">() =&gt;</span> {
    fetch(url, {
        <span class="hljs-attr">headers</span>: {
            <span class="hljs-attr">Authorization</span>: <span class="hljs-string">`bearer <span class="hljs-subst">${process.env.env_variable}</span>`</span>
        }
    })
    .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
    .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
    .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">JSON</span>.stingify(err))
}
</code></pre>
<p>This error happens because <code>require</code> is not in the <code>node_module</code> or list of packages that'd make the <code>dotenv</code> package function. </p>
<p>In a nutshell, <code>dotenv</code> needs <code>require</code> to function. You can get <code>require</code> from <a target="_blank" href="https://requirejs.org/">RequireJS</a>, but that's another hassle there. You would have to read through the docs on how to apply the scripts that would make Node's global variable available on the client-side.</p>
<h2 id="heading-why-go-through-all-that-hassle">Why go through all that hassle?</h2>
<p>Really. Why?</p>
<p>Folks typically use public APIs either for a personal project or to mess around with some concepts that they haven't quite gotten the hang of. </p>
<p>Most times, these APIs do not require the use of private (API) keys for one authentication or the other. This is common when you're dealing with endpoints that allow only the <code>GET</code> method of fetching data.</p>
<p>APIs like GitHub's or Twitter's require the use of api_keys to authenticate the user before they allow the request to go through. The GitHub GraphQL API, for instance, requires an access token for a successful API call. But the access token has some quirks, one of which is the ability to perform 5000 requests in an hour. </p>
<p>You can never commit this access token into the Git workflow of your project. If you do commit it, GitHub will delete it for security reasons. This is where it becomes an issue that VanillaJS can't hold environment variables. </p>
<p>The access token provided by GitHub (that eventually gets deleted once it is committed into the workflow) will not allow the application to function in <code>production</code> mode. It'd work perfectly fine in <code>development</code> – but once it is deleted, and the repository/project is deployed to Netlify, then Netlify can't access the keys again.</p>
<h2 id="heading-how-do-you-resolve-this-issue">How do you resolve this issue?</h2>
<p>Netlify has a "build and deploy" settings tab. This allows you to alter how the continuous deployment process takes place with your projects or repositories on GitHub. </p>
<p>You can decide to stop all the concurrent automatic builds once Netlify detects a push to the <code>master</code> or <code>main</code> branch, deactivate all builds until the project is done completely on development mode, and many more features that I can't remember.</p>
<p>But, that's not the focus of this article. What we're concerned about is how to use the GitHub access token locally (by ensuring that it doesn't get into the commit history) and then allow Netlify to have access to it, in <code>production</code>.</p>
<p>The image below shows the "build and deploy" tab on Netlify.  </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/Screenshot-from-2021-05-31-10-39-42.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Notice the build command input field? Using the code snippet below:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> js &amp;&amp; <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"const TOKEN = 'api-token';\n\nexport default TOKEN;"</span> &gt; config.js
</code></pre>
<p>the command above will simply inject a new file called, <code>config.js</code> inside the <code>js</code> folder during the build process. This gives Netlify access to your API key (access token). </p>
<p>If there is no <code>js</code> folder in your project, that is you have all files in the root folder of the project, you can simply add <code>echo -e "const TOKEN = 'api-token';\n\nexport default TOKEN;" &gt; config.js</code> as the build command.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> TOKEN = <span class="hljs-string">'api-token'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> TOKEN;
</code></pre>
<p>To make sure you're able to use the ES6 <code>import</code> statement in the JavaScript file, you need to add the <code>type="module"</code> attribute in the script tag</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./index.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This might not seem like the best practice or method for using environment variables. This is because your API key might still be visible to anyone who views or visits your app on the internet when they open the devtools on their favourite browser. </p>
<p>But it helped me bypass the issue of GitHub deleting these keys which will in turn stop the application from working in <code>production</code>. </p>
<p>You should only consider this method when you're using an API that, when your API key is revealed, won't cause much harm when it is used by a third party.</p>
<p>Thank you for reading this article. I hope it helps. ;)  </p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
