Suggest an editImprove this articleRefine the answer for “Environment variables”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Environment variables** are key-value pairs stored by the operating system and exposed through `process.env`; they exist so configuration (ports, keys, passwords) doesn't have to live directly in the code. **Key point:** for local development, values usually live in a `.env` file loaded via the `dotenv` package, and `.env` itself is never committed to the repository.Shown above the full answer for quick recall.Answer (EN)Image## 1. What environment variables are **Environment variables** are **key-value pairs** stored **by the operating system**, available to any process you launch. They exist so **configuration doesn't live inside the code** (especially sensitive data). ### Example on Linux / macOS: ```javascript export NODE_ENV=production export PORT=3000 ``` ### Example in Windows PowerShell: ```javascript $env:NODE_ENV="development" $env:PORT="8080" ``` After that, the variables are available to every process started from that session. ## 2. Reading environment variables in Node.js Node.js exposes a global `process.env` object holding **every** environment variable: ```javascript console.log(process.env.NODE_ENV); // 'production' console.log(process.env.PORT); // '3000' ``` If a variable doesn't exist, it's `undefined`: ```javascript console.log(process.env.API_KEY); // undefined ``` ## 3. Setting variables at launch You can set variables **right in the run command**. ### Linux / macOS: ```javascript NODE_ENV=production PORT=8080 node app.js ``` ### Windows (cmd.exe): ```javascript set NODE_ENV=production && set PORT=8080 && node app.js ``` ### Windows (PowerShell): ```javascript $env:NODE_ENV="production"; $env:PORT="8080"; node app.js ``` ## 4. Using variables in code They're usually used to configure the app: ```javascript const PORT = process.env.PORT || 3000; const NODE_ENV = process.env.NODE_ENV || 'development'; app.listen(PORT, () => { console.log(`Server running on port ${PORT} in ${NODE_ENV} mode`); }); ``` If a variable isn't set, it's best to provide a **fallback default**. ## 5. Working with a `.env` file (dotenv) Storing variables directly in the OS is inconvenient, especially during development. So a `.env` file is usually created at the project root instead. ### .env ```javascript NODE_ENV=development PORT=4000 API_KEY=12345-ABCDE DB_URL=postgres://user:pass@localhost/db ``` ### Installing the package: ```javascript npm install dotenv ``` ### Loading it in code: ```javascript import dotenv from 'dotenv'; dotenv.config(); console.log(process.env.DB_URL); ``` After `dotenv.config()` runs, every variable from `.env` is added to `process.env`. ## 6. Why environment variables matter | Reason | Example | |---|---| | Separating environments | `NODE_ENV=development` / `production` | | Security | API keys, passwords, tokens don't live in the code | | Flexible configuration | Behavior can change without touching the source | | Portability | The same code runs on different systems (dev/staging/prod) | | CI/CD integration | Variables are passed through GitHub Actions, Docker, etc. | ## 7. A full example scenario ### .env ```javascript NODE_ENV=production PORT=8080 API_KEY=abc123 ``` ### app.js ```javascript import dotenv from 'dotenv'; dotenv.config(); console.log('ENV:', process.env.NODE_ENV); console.log('API KEY:', process.env.API_KEY); console.log('PORT:', process.env.PORT); if (process.env.NODE_ENV === 'production') { console.log('Running in production mode'); } else { console.log('Running in development mode'); } ``` ### Running it ```javascript node app.js ``` **Output:** ```javascript ENV: production API KEY: abc123 PORT: 8080 Running in production mode ``` ## 8. Where environment variables are used | Area | Examples | |---|---| | Server settings | PORT, HOST, BASE_URL | | Security | JWT_SECRET, API_KEY, DB_PASSWORD | | Databases | DATABASE_URL, MONGO_URI, REDIS_HOST | | Integrations | AWS_ACCESS_KEY, STRIPE_SECRET_KEY | | Runtime mode | NODE_ENV=development / production | ## 9. Securing `.env` - Don't push `.env` to GitHub, add it to `.gitignore` instead: ```javascript .env ``` - For deployment, store variables in the platform's environment settings (Heroku, Vercel, Docker, etc.) - Never log sensitive data (`process.env.PASSWORD`). ## 10. Environment variables in Docker In a `Dockerfile`: ```javascript ENV NODE_ENV=production ENV PORT=8080 ``` or in `docker-compose.yml`: ```javascript environment: - NODE_ENV=production - PORT=8080 ``` Node.js inside the container still reads them through `process.env`. ## 11. Quick summary | Point | Description | |---|---| | What it is | Keys and values defined in the process's environment | | Access | `process.env.VARIABLE` | | Setting them | Via `export` / `set` / `.env` | | Library | `dotenv` for local files | | Goal | Safe, flexible application configuration | | Important | Never store `.env` in the repository | ## In one sentence: > **Environment variables** are a way to pass external configuration into Node.js (mode, ports, keys, passwords, and so on) through `process.env`, so the code stays generic, secure and independent of where it runs.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.