DEV Community

nermineslimane
nermineslimane

Posted on

What is .env ? How to Set up and test 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

Image description

  1. Commit the changes to your repository
git add .gitignore
git commit -m "Adding .env to .gitignore"
Enter fullscreen mode Exit fullscreen mode
  1. Install npm package dotenv
npm i dotenv
Enter fullscreen mode Exit fullscreen mode
  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

Image description

Then in our entry point we're testing if the STATUSis production we're going to use the PROD_PORT else we're using the DEV_PORT

Image description

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

Image description

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

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)