What is .env ? How to Set up and test a .env file in Node?
Tags: node, javascript, webdev, beginners
Published: 2022-01-17 10:30:31

## 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 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g18ljfiwnzbgij33x84.png)

2. Commit the changes to your repository 

```
git add .gitignore
git commit -m "Adding .env to .gitignore"
```

3. Install npm package [`dotenv`](https://www.npmjs.com/package/dotenv)

```
npm i dotenv
```
4. 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 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dhw6zxqs0czsyel6hy1.png)

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`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfus3kwiabm7m59e50mt.png)

5. Run the application  
Change the status variable in your .env and see what happens 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ampvva436evn5ds1eurt.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 !