.env file make in nodejs
Install the
dotenv
package using npm:bash npm install dotenv
Create a
.env
file in the root of your Node.js project.Add your environment variables to the
.env
file. For example:DB_HOST=localhost DB_USER=admin DB_PASSWORD=secret
In your Node.js application, require the
dotenv
package at the top of your main file:javascript require('dotenv').config();
Access the environment variables in your code using
process.env
:javascript const dbHost = process.env.DB_HOST; const dbUser = process.env.DB_USER; const dbPassword = process.env.DB_PASSWORD;
Use the variables in your application as needed.
Note: Make sure to add the .env
file to your .gitignore
to avoid exposing sensitive information, and provide a sample .env.example
file with placeholders for others to follow.