What are Environment Variables?
Environment variables are dynamic named values that can affect how running processes behave on a computer.
They are part of the environment in which a process runs and are used to configure applications without changing the code.
Note: Key Benefits: Store configuration separate from code Keep sensitive information out of version control Configure applications differently across environments Change application behavior without code changes
Common Use Cases
Accessing Environment Variables in Node.js
Node.js provides the process.env object to access environment variables.
This object contains all the environment variables available to the current process.
Basic Usage
// Access a single environment variable
const nodeEnv = process.env.NODE_ENV || 'development';
console.log(`Running in ${nodeEnv} mode`);
// Access multiple variables with destructuring
const { PORT = 3000, HOST = 'localhost' } = process.env;
console.log(`Server running at http://${HOST}:${PORT}`);
// Check if running in production
if (process.env.NODE_ENV === 'production') {
console.log('Production optimizations enabled');
// Enable production features
}
Common Built-in Environment Variables
| Variable | Description | Example |
|---|---|---|
NODE_ENV |
Current environment (development, test, production) | production |
PORT |
Port number for the server to listen on | 3000 |
PATH |
System path for executable lookup | /usr/local/bin:/usr/bin |
HOME |
User's home directory | /Users/username |
Note: Always provide default values when accessing environment variables to prevent
undefinedvalues in your application.
Setting Environment Variables
There are several ways to set environment variables for your Node.js application, depending on your development workflow and deployment environment.
1. Command Line (Temporary)
Set variables directly in the command line when starting your application:
Windows (Command Prompt)
set PORT=3000
set NODE_ENV=development
set DB_HOST=localhost
node app.js
2. Using .env Files with dotenv
For development, use a .env file to store environment variables locally:
1. Install dotenv package
npm install dotenv
Warning: Important: Never commit
.envfiles to version control. Add.envto your.gitignorefile.
3. Production Environment Variables
In production, set environment variables using your hosting provider's configuration:
Heroku
heroku config:set NODE_ENV=production DATABASE_URL=your_database_url
Using dotenv for Local Development
The dotenv package lets you load environment variables from a .env file:
# .env file
API_KEY=abcdef12345
Load the variables in your app:
require('dotenv').config();
console.log(process.env.API_KEY);
Install dotenv with:
npm install dotenv
Summary
Environment variables help you keep sensitive data and configuration out of your code.
Use process.env and tools like dotenv to manage them easily in Node.js.