Working of dontenv
1. Loading Environment Variables: Dotenv simplifies the process of loading environment variables from a ‘.env’ file into Node.js applications. The ‘.env’ file typically resides in the root directory of the project. It stores key-value pairs of environment variables in a simple format: KEY=VALUE.
_PORT = 3000
API_KEY = localhost
API_PASS= mypassword
2. Parsing and Setting Environment Variables: When you launch your Node.js application, dotenv steps into ‘.env’ file. It reads its contents, and creates individual settings (environment variables) within your program. Through these settings, environment variable are easily accessible throughout your code using process.env.VARIABLE_NAME.
const PORT = process.env.PORT
const API_KEY = process.env.API_KEY
Now make a Project to show use of dotenv for better understanding.
NPM dotenv
Dotenv is a popular npm (Node Package Manager) package used in NodeJS applications to manage environment variables. Environment variables are values that are set outside of an application’s code and are accessible to the application during runtime. These variables often contain sensitive information like API keys, database connection strings, or configuration settings.
Contact Us