bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Node.js/Node.js Deployment
Node.js•Node.js Deployment

Node.js Environment Variables

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Node.js Environment Variables?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ a single environment variable
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Setting Environment Variables
Common Built-in Environment Variables
Accessing Environment Variables in Node.js

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.

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

  • Database connection strings
  • API keys and secrets
  • External service URLs
  • Feature flags
  • Logging verbosity
  • Port numbers
  • Timeouts and limits
  • Environment-specific settings

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

VariableDescriptionExample
NODE_ENVCurrent environment (development, test, production)production
PORTPort number for the server to listen on3000
PATHSystem path for executable lookup/usr/local/bin:/usr/bin

Note

Always provide default values when accessing environment variables to prevent undefined values 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.

Command Line (Temporary)

Set variables directly in the command line when starting your application:

set PORT=3000
set NODE_ENV=development
set DB_HOST=localhost
node app.js
$env:PORT=3000
$env:NODE_ENV="development"
node app.js
PORT=3000 NODE_ENV=development DB_HOST=localhost node app.js
export PORT=3000
export NODE_ENV=development
node app.js

Using .env Files with dotenv

For development, use a .env file to store environment variables locally:

npm install dotenv
# .env
PORT=3000
NODE_ENV=development
DB_HOST=localhost
DB_USER=admin
DB_PASS=your_secure_password
API_KEY=your_api_key_here
// Load environment variables from .env file
require('dotenv').config();
const port = process.env.PORT || 3000;
const dbConfig = {
 host: process.env.DB_HOST,
 user: process.env.DB_USER,
 password: process.env.DB_PASS
};
console.log(`Server running on port ${port}`);

Important: Never commit .env files to version control. Add .env to your .gitignore file.

Production Environment Variables

In production, set environment variables using your hosting provider's configuration:

heroku config:set NODE_ENV=production DATABASE_URL=your_database_url
docker run -e NODE_ENV=production -e PORT=3000 your-image
# /etc/systemd/system/your-app.service
[Service]
Environment="NODE_ENV=production"
Environment="PORT=3000"

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.

Next

Node.js: Development vs Production