🚀 Express.js
Intermediate
What is the dotenv package and how is it used in Express?
Answer
dotenv loads environment variables from a .env file into process.env, keeping sensitive configuration (database URLs, API keys, JWT secrets) out of source code. Install: npm install dotenv. At the very top of your entry file: require('dotenv').config();. Your .env file: DB_URL=mongodb://localhost/mydb\nJWT_SECRET=supersecret\nPORT=3000. Access: process.env.DB_URL. Add .env to .gitignore — never commit it. Provide a .env.example with placeholder values as documentation. In production (hosting platforms like Heroku, Render, AWS), set environment variables directly in the platform settings rather than a .env file.
Previous
How do you structure a large Express.js application?
Next
How do you implement pagination in an Express REST API?
More Express.js Questions
View all →- Intermediate How do you implement JWT authentication in Express.js?
- Intermediate What is Express middleware chaining and how does it work?
- Intermediate What is the helmet package and why should you use it?
- Intermediate How do you implement rate limiting in Express.js?
- Intermediate What is input validation and how do you do it in Express?