How to Use Environment Files (.env) in Node.js | Tushar Khatri
Learn all about dotenv npm package and .gitignore file to keep our secrets safe in an express.js app.

Search for a command to run...
Learn all about dotenv npm package and .gitignore file to keep our secrets safe in an express.js app.

No comments yet. Be the first to comment.
The best way to send automated emails from your website.

There are several options when it comes to programming languages, and deciding which one to use entails a lot of considerations. Choosing the right programming languages is half the problem done, some may choose Java, some prefer Python, while some o...

There are more than a few units to size an element using CSS. You might have wondered which one to use. let me help you choose the right one for your needs. Difference Between PX, EM, REM, and % let me make it simple for you: 16px = 1em = 1rem = 100%...

When I was learning back-end development I encountered some major problems while hosting my apps, one of these was how to upload the code to a remote repository and at the same time prevent my secrets from being exposed.
Data such as
API keysorDatabase URIor any key from cloud services likeAWSorGoogle Cloudare counted as secrets and are not to be exposed under any circumstance.
We need to learn about 2 things in order to keep our secrets unexposed i.e.
dotenv is a npm package, it helps us store secrets in a separate file called .env
npm i dotenv from the root of the project to install the package.require('dotenv').config();. Make sure to require it at the very top of your app.js file so that it can be used throughout the project..env file in your root and add all the secrets in this file as shown below:API_KEY=anyApiKey
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
SECRET_HASH="something-with-a-#-hash"
You must use double quotes when reserved keywords like hash are appearing in the secret.
process.env.API_KEY.you just got to add your variable name after process.env.
//import and configure dotenv at the very top of your file.
require('dotenv').config();
app.get((req, res) => {
const apiKey = process.env.API_KEY;
const secretKey = process.env.SECRET_KEY;
const secretHash = process.env.SECRET_HASH;
console.log(`Secrets stored in .env file are ${apiKey} and ${secretKey} and ${secretHash}`);
});
.gitignore, exclude unwanted files from git tracking.Now once you have your secrets in place you need to make sure that these variables must not appear in your commit history. Whenever you commit a change git saves all the newly added files and lines of code. We can ignore tracking of these files by following the steps below:
.gitignore file in the root of your project./node_modules
.env
Adding .env in the .gitignore file tells git to ignore this file and any changes made to this file. Thus, making your secrets real secrets. Likewise by adding /node_modules git will ignore the whole folder.
.env file to the cloud or hosting platform?Every hosting provider has a tab called Variable where you can add the secrets and your app will have access to them.

You don't need to upload .env file anywhere, we use the .env file to access and store our secrets separately in our local environment and to safeguard them while uploading code to remote repos.
If this article was helpful please consider following me on Instagram or GitHub. If you wish to find more such articles in your inbox please consider subscribing to my newsletter. Your appreciation is my fuel 😌.