Create and use environment variables in your nodejs project

Create your environment variables and use them across the nodejs project.

  idkblogs.com      March 11, 2021

Shubham Verma

Shubham Verma


Create and use environment variables in your nodejs project

E nvironment variables are very important part of your project. It gives you flexibility, adoption extendability, and security to your project. It gives you better testing approach for the deployment. You can test on local with a given set of data so that your other deployment data and flow will be untouched. In this article, we will create environment variables step by step and use them across the nodejs app.

Why environment variables are necessary in your nodejs app?

Environment variables are necessary because of different deployment like local,staging, development and production, and we can't use one set of data for all the deployment locations. Sometime we need to test our nodejs app with different set if data. The data can we anythings like, project secrets, keys, passwords, emails, it can be anything. In my case, I was working on a project where I need to send set of emails to the users. So I can not send email to users in my testung stage. I need to test the features with some test email ids. so if I ran the nodejs project on my local then I need some test data to test. I can not test with original data. Thst's why we need environment variables.
In short, we restrict the data uses by app using the environment variables.

Let's learn how to create the environment variables first

First, we need to create our environment variables. to do this create a file with name .env on root level. We will have our all the environment variables in this file. we can create more environment variables based on our deployment location(s). Also we can use this environment variables through the application or using by some given tools. Here we will use through application. We can use application level tool like dotenv. But here we'll not use any modules. we'll write all the codes to use the environment variables. A .env file is more or less a plain text document.
So write some information on our .env file:

.env:

Note: You need to have the data in .env as per the deployment locations. And the data can be different in .env file in ech locations.

Its time to write some utility functions to read and set the environment variables in your nodejs app

Let's create a utility functions and export this function for further use.

Use the setProcessENV method very early of your node app initialization:

Now we need to use the setProcessENV method very early when your server start, The best way in app.js or server.js file.

import setProcessENV method and call the setProcessENV. So add below line in your app.js

app.js

Access and use the environment variables through the nodejs project:

Now our environment variables are set, we can use this environment variables as per the requirements. Use the env in this way: Suppose you want to do some task only for production:

Conclusion:

In this article, we saw how we can create our environment variables and how we can use this environment variables as per our requirements. In short:

// get an environment variable at runtime
export const ANYTHING = process.env['ANYTHING'];

// set an environment variable at runtime
process.env['ANYTHING'] = 'ANYTHING'


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.