How to configure environment variables in node application

While building an application we come across several situations where we need to use several confidential data such as API keys, Secrets and Many More. But is it safe to put this data directly into the Code?

Well the answer is NO!

For stori…


This content originally appeared on DEV Community and was authored by Himanshu

While building an application we come across several situations where we need to use several confidential data such as API keys, Secrets and Many More. But is it safe to put this data directly into the Code?

Well the answer is NO!

For storing such variables we have so called environment variable. You might have come across the term earlier or maybe not.

What are environment variables?

Those variables whose values are set outside the project and is accessible across whole project is known as Environment Variables.

Setting up the Project

For learning the concept of Environment Variables setup the project as shown below.

- index.js
- config
    - .env

After this open the terminal and cd into the root of the project i.e. Folder containing the above files. We will initiate an npm project by writing the following command

npm init -y

After the command is completed the file directory will look as follows:

- index.js
- config
    - .env
- package.json

DOTENV

For configuring environment variables we will be using an NPM package called DOTEVN.

image

Installing the Package

Use npm install dotenv or npm i dotenv to install the package.

Package in Use

Now you can configure this package in which ever file you like but it is suggested to configure it as soon as possible.

This is so because if you are using the environment variable before configuration of the package then your app may crash or work unexpected.

    require('dotenv').config({ path: './config/.env' });

You index.js should look as shown above. The path property should be specified with respect to file location you are writing the require statement.

Setting Up the .ENV file

IT is essential to write variable name in all capital letters and the value for the variable should be written in plane text without any quotation marks.

VARIABLE_NAME=VALUE

For eg: You want to set up your PORT at 5000 or Apikey to "helloworld" then the code in .env file will be

PORT=3000
API_KEY=helloworld

Accessing the Variable Value

The environment variables are set in env object as a property of process parameter.

Hence The variable can be accessed through

process.env.VARIABLE_NAME

Predefined Environment Variables

The system has some pre-defined environment variables which you can access through the same method i.e. process.env.VARIABLE_NAME.

To know the predefined environment variable try console logging the whole environment object i.e.

console.log(process.env)

HELP

I would highly appreciate if you leave a feedback, criticism or suggestion of any kind.

I'm a computer engineering student ? from India who loves to code.

An avid and passionate coder specializing in different languages. I love to build and design websites which the end user would enjoy using while keeping the website performant and the code clean. Up for freelance web development work and collaborating on exciting Open Source & Personal projects.

⚡ Stuff I Know


? Stuff To Explore



Happily turning coffee into code!✅

If you're interested in contributing, the projects is open-source and I would appreciate any sort of help. Otherwise, you can share it or star the repo, if you want to of course.


This content originally appeared on DEV Community and was authored by Himanshu


Print Share Comment Cite Upload Translate Updates
APA

Himanshu | Sciencx (2021-05-19T07:23:03+00:00) How to configure environment variables in node application. Retrieved from https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/

MLA
" » How to configure environment variables in node application." Himanshu | Sciencx - Wednesday May 19, 2021, https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/
HARVARD
Himanshu | Sciencx Wednesday May 19, 2021 » How to configure environment variables in node application., viewed ,<https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/>
VANCOUVER
Himanshu | Sciencx - » How to configure environment variables in node application. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/
CHICAGO
" » How to configure environment variables in node application." Himanshu | Sciencx - Accessed . https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/
IEEE
" » How to configure environment variables in node application." Himanshu | Sciencx [Online]. Available: https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/. [Accessed: ]
rf:citation
» How to configure environment variables in node application | Himanshu | Sciencx | https://www.scien.cx/2021/05/19/how-to-configure-environment-variables-in-node-application/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.