DEV Community

Cover image for Environment Variables in Node.js
Benjamin Nwokolo
Benjamin Nwokolo

Posted on

Environment Variables in Node.js

Environment Variables are very useful and helpful in the configuration of applications , in This Article We are going to discussing about Environment Variables and How to work with Environment Variables in Node.js Applications.

Table Of Contents

  • Introduction to Environment Variables / What are Environment Variables.

  • Importance and Use cases of environment variables

  • Getting started with Environment Variables in Node.js

  • Uses of Environment Variables in Node.js

  • Working With Environment Variables in Node.js

  • Security Practices in an .env file

  • How To Access environment variables stored in a .env file within a Node.js Application.

Introduction to Environment Variables / What are Environment Variables.

Environment Variables helps in the configuration of applications in different environment in which an application is running on, be it a Production , Development or a Testing environment.
Environment Variables are Variables set by the operating system which helps to create the environment that a computer program runs on , one of the commonly used environment variable is the PATH Environment Variable . As A Developer when building applications you should never store and write sensitive information such as API Keys ,HOSTS, Database Credentials , Database id and Passwords in Your Code which would be pushed to public, with the help of environment variables such sensitive information can be hidden and be protected to avoid any security breach .

Importance and Use cases of environment variables

  • Environment Variables are useful when separating Programming and configuration settings of an application ,which makes it easier in Deploying an application to different environments. for instance the Database configuration settings of an Application in Development would be different from the settings in Production , and this configuration settings can be stored as environment variables.

  • Environment Variables Helps in the protection and security of configuration settings for example API Keys and Passwords should not be hardcoded in an application in production instead it should added as an environment Variable .

Getting started with Environment Variables in Node.js

Environment Variables can be used in the configuration of Node.js Applications , We can store sensitive information such as passwords and Credentials as environment variables in Node.js .

Uses of Environment Variables in Node.js

  • Environment Variables are used in Hiding and Protecting API Keys and Passwords.

  • Environment Variables are used in setting the PORT of Node applications.

  • Environment Variables are used in configuring Database Settings.

Working With Environment Variables in Node.js

Environment Variables in Node are accessed using the process object , which is a Global object that is injected during runtime which represents a node process , one of the major property is the env propety ,an env property provides access to all existing environment variables in a Node.js application


//process object
process.env

Enter fullscreen mode Exit fullscreen mode

process.env is a global object in Node.js from which we can access all the environment variables in the environment in which an application is running on , this object returns properties which contains the user environment and form the object we have access to the PATHand PORTamongst other properties .

console.log(process.env);
Enter fullscreen mode Exit fullscreen mode

When it is been logged to the console we would have access to the various properties of the process.env object. the Most Efficient Way to provide environment variables in Node.js is through an .envfile

Using the .env file

.env file is a text file which contains a set of key value pairs of environment variables , we can place the value of an application passwords , API Keys and Database Credentials which we want to keep private in an .env file. .env file generally lets us store values in it , values in which we do not want to embedded / placed inside code which can be accessed or seen by the public.

So lets create a .env file in our application directory

Image description

now lets store an example of configuration settings, API keys Database Passwords and Credentials inside this .env file we created:

DB_USERNAME = Benjamin
DB_PASSWORD = Ben102722
API_KEY = DHDJHWRR66
API_PASSWORD = SHD2YWO21093J2Q92Q03
Enter fullscreen mode Exit fullscreen mode

We Have now stored environment variables (in the form of key value pairs )inside the .env file which we can access later on in our application .

Security Practices in an .env file

.env files should be made private and should not be shared with anyone and also should not be made accessible to the public because it contains sensitive informations such as Database Credentials ,API Keys e.t.c . env files should not be visible in the source code when uploading codes to version control services such as Github.
To Be able to prevent and exclude .env files From being pushed to Github and been made accessible to the public
, we create a .gitignore file in the application codebase and exclude the .envfile from being pushed to github , and it is done by typing .env inside the .gitignore file , so that git would ignore the file when it is been pushed

Image description

How To Access environment variables stored in a .env file within a Node.js Application.

To make use of the environment variables stored in the .env file , we make use of 3rd Party NPM Package to access the variables and the package is called dotenv .
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.
dotenv is a npm package that loads environment variables and helps read .env files.
To install dotenv , Head over to the command line terminal and execute this command in the directory which contains the application to be able to install it :
npm install dotenv --save

After successfully installing it , to be able to access the environment variables stored in the .env file, head over to file in the which you will making use of the variable in and require dotenv require('dotenv').config(); with this, we can now proceed to access all the environment variables in which we stored in the .env file above. So lets access the DB_PASSWORD stored in the .env file as an a example:

const express = require('express')
const path = require ('path');
require('dotenv').config();

//accessing the DB_PASSWORD from the .env File
console.log(process.env.DB_PASSWORD);

const app = express();
app.listen(process.env.PORT || 3000);
Enter fullscreen mode Exit fullscreen mode

when the code is executed we get the exact value of the DB_PASSWORD as it was stored in the .env file .

Image description

Conclusion

This Article has explained and shown the Basics and the use cases of environment variables and the .env file in a Node.js Application . I Hope You Found this article Helpful !

Top comments (0)