"Learn how to manage environment-specific configurations and secrets in your development projects using .env files."
Abhishek Kumar
Environment variables are a simple and powerful way to manage sensitive or environment-specific data in your applications. Using a .env file can help keep your code clean, secure, and portable.
A .env file is a plain text file that stores environment variables in a KEY=VALUE format. It’s not part of your source code but holds configuration data your app can access during runtime.
Example:
API_KEY=12345abcdef DB_URL=mongodb://localhost:27017/myapp PORT=3000
npm install dotenv
At the top of your index.js or app.js:
require("dotenv").config();
console.log(process.env.API_KEY);
Instead of hardcoding:
const apiKey = process.env.API_KEY;
Using .env files is a small but impactful step toward writing clean, maintainable, and secure code. Start using them today to manage your app's configuration the smart way!