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"
- Install npm package dotenv
npm i dotenv
- 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
1.Run the application Change the status variable in your .env and see what happens
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 !
Please help me
I’m creating a mobile app for pi network but something becomes hard to me to fix
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’) })
write this step of import also : require(‘dotenv’).config();
0_0