Codementor Events

What is .env ? How to Set up and run a .env file in Node?

Published Jan 20, 2022Last updated Jul 18, 2022
What is .env ? How to Set up and run a .env file in Node?

What are environment variables ?

Environment variables offer information on the process's operating environment (producton, development, build pipeline, and so on). Environment variables in Node are used to store sensitive data such as passwords, API credentials, and other information that should not be written directly in code. Environment variables must be used to configure any variables or configuration details that may differ between environments.

Environment variables are already included in the Node.js ecosystem, which gives them a significant benefit over alternative configuration choices such as a config.js or config.json file. Environment variables, especially when used in conjunction with automation, such as a build pipeline, allow you to avoid doing unpleasant things like scripting configuration files.

Now let's dive in some coding and practice !

How to Set up and read a .env file ?

The dotenv package for handling environment variables is the most popular option in the Node.js community. You can create an.env file in the application's root directory that contains key/value pairs defining the project's required environment variables. The dotenv library reads this.env file and appends it to process.env. Please do not save your.env file on your computer.
In five easy steps, we'll update.gitignore, create a.env file, and read it:
1. Add .env to gitignore

# dotenv environment variables file
.env
.env.test

1. Commit the changes to your repository

git add .gitignore
git comit -m "adding .env.to .gitinore"
  1. Install npm package dotenv
npm i dotenv
  1. It's time to use our env variables
    Add some variable to your .env file, for exemple we're going to add a status for our nodejs app and define two different ports, one for development status and one for production
1 STATUS =production
2 DEV_PORT =7000
3 PROD_PORT = 8000
4

Then in our entry point we're testing if the STATUS is production we're going to use the PROD_PORT else we're using the DEV_PORT
port.png

1.Run the application Change the status variable in your .env and see what happens
port 2.png

It is excellent practice to document the.env file with an example. The.env file should be particular to the environment and not checked into version control. This.env.example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.

This is an example of a .env.example:

# Environment variables.
STATUS=production
#Development port
DEV_PORT=7000
#Production port
PROD_PORT=8000

#DB CONFIG
HOST=db.host
USER=root
PASSWORD=db.password
DB=db.name
DIALECT=mysql

Thanks for reading and if you have any questions, use the comment function !

Discover and read more posts from Parthibakumar Murugesan
get started
post commentsBe the first to share your opinion
Hashimu Shabani
3 months ago

Please help me
I’m creating a mobile app for pi network but something becomes hard to me to fix

Martin Fallenstedt
3 months ago

Thank you for this almost complete article. In order to make DOTENV work there is also a need to add the following code :

const path = require(‘path’)
require(‘dotenv’).config({ path: path.resolve(__dirname, ‘…/.env’) })

Vedansh Verma
5 months ago

write this step of import also : require(‘dotenv’).config();
0_0

Show more replies