What is the dotenv package and how is it used?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Node.js topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
dotenv is a zero-dependency npm package that loads environment variables from a .env file into process.env. This allows you to configure your application without hardcoding sensitive values (database passwords, API keys, secret tokens) in source code. Usage: (1) Install: npm install dotenv; (2) Create .env file: DB_PASSWORD=secret\nPORT=3000; (3) Load at app entry point: require("dotenv").config(); (must be called before accessing process.env); (4) Access: process.env.DB_PASSWORD. Always add .env to .gitignore to prevent committing secrets. Provide a .env.example file with placeholder values as documentation for teammates. Node.js 20.6+ has native --env-file flag: node --env-file=.env app.js, reducing the need for dotenv in newer projects.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Node.js project, I used this when...' immediately makes your answer more credible and memorable.