What is the dotenv package and how is it used?

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.