🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Node.js Notes
Topic #78

Node Env Variables


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 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.

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 .env files to version control. Add .env to your .gitignore file.

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.

Want to go beyond the notes?

Join CodingNow 2.0's Node.js course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Node Env Variables – FAQs

Quick answers about learning Node Env Variables in Node.js.

This free note from CodingNow 2.0 explains Node Env Variables in Node.js — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Node.js topic on CodingNow 2.0, including Node Env Variables, is 100% free with no signup required.
With focused practice, most students grasp Node Env Variables in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now