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 passw…


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

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

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

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


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


Print Share Comment Cite Upload Translate Updates
APA

nermineslimane | Sciencx (2022-01-17T10:30:31+00:00) What is .env ? How to Set up and test a .env file in Node?. Retrieved from https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/

MLA
" » What is .env ? How to Set up and test a .env file in Node?." nermineslimane | Sciencx - Monday January 17, 2022, https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/
HARVARD
nermineslimane | Sciencx Monday January 17, 2022 » What is .env ? How to Set up and test a .env file in Node?., viewed ,<https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/>
VANCOUVER
nermineslimane | Sciencx - » What is .env ? How to Set up and test a .env file in Node?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/
CHICAGO
" » What is .env ? How to Set up and test a .env file in Node?." nermineslimane | Sciencx - Accessed . https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/
IEEE
" » What is .env ? How to Set up and test a .env file in Node?." nermineslimane | Sciencx [Online]. Available: https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/. [Accessed: ]
rf:citation
» What is .env ? How to Set up and test a .env file in Node? | nermineslimane | Sciencx | https://www.scien.cx/2022/01/17/what-is-env-how-to-set-up-and-test-a-env-file-in-node/ |

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.