Environment variables are a must-have in Node.js apps to keep sensitive info (API keys, DB credentials) and environment-specific settings out of your codebase. Letβs go step by step.
π 1. Install dotenv
npm install dotenv
π 2. Create a .env
file
This lives in your project root (and should be gitignored):
PORT=4000
MONGO_URI=mongodb://localhost:27017/mydb
JWT_SECRET=supersecretkey
NODE_ENV=development
β‘ 3. Load .env
in Node.js
In your entry point (server.js
or app.js
):
require("dotenv").config();
console.log("PORT is:", process.env.PORT);
console.log("Environment:", process.env.NODE_ENV);
π οΈ 4. Using Variables in Your App
Example with Express and MongoDB:
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("DB connected"))
.catch(err => console.error("DB error:", err));
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}`);
});
π 5. Security & Best Practices
-
Never commit
.env
files β add.env
to.gitignore
. -
Use
.env.example
to show required vars without secrets:PORT= MONGO_URI= JWT_SECRET= NODE_ENV=
-
Keep different
.env
files per environment:-
.env.development
-
.env.production
-
π Then load the right one based on NODE_ENV
.
β In Short
-
Use
dotenv
to load.env
files intoprocess.env
. -
Store secrets and environment-specific configs there.
-
Never commit real
.env
files to git β use.env.example
. -
Access vars via
process.env.KEY
.