In Node.js, process.env is a global object that provides access to environment variables that are key-value pairs from the operating system used to control application behavior across different environments without changing the code. This helps us in following ways:
- Keeps sensitive data like API keys outside the codebase
- Supports different setups for development, testing, and production
- Improves portability across systems and deployment environments
- Centralizes configuration, making apps easier to manage and scale
Syntax :
process.env.VARIABLE_NAMEwhere,
process:Node.js global object representing the current processenv: Object that stores all environment variablesVARIABLE_NAME:Specific environment variable key you want to access
Return Value :
This property returns an object containing the user environment.
The process.env property is an inbuilt application programming interface of the process module which is used to get the user environment. It gives the string output from the environment variables.
Why Use Environment Variables
Environment variables offer several advantages, including:
- Separation of Concerns: Environment variables separate configuration from application logic, making code cleaner and easier to maintain.
- Security: Sensitive data like API keys or database credentials isn’t hardcoded.
- Portability: Applications can run across different environments without code changes.
- Version Control Safety: Variables stay outside the codebase, so they aren’t exposed in version control.
- Flexibility: Values can be updated easily without modifying or redeploying the code.
Using process.env property in Node.js
Setting Environment Variables
1. Unix/Linux/macOS: You can set environment variables inline:
NODE_ENV=production PORT=3000 node app.js2. Windows: Set environment variables using the set command:
set NODE_ENV=production && node app.jsReading Environment Variables
We can read the environment variable using:
process.env.VARIABLE_NAMEExample 1: This example console log the process.env object.
// Filename - index.js
// Node.js program to demonstrate the
// process.env Property
// Include process module
const process = require('process');
// Printing process.env property value
console.log(process.env);
Output:
{
OS: 'Windows_NT',
USERNAME: 'gekcho',
USERPROFILE: 'C:\\Users\\gekcho',
HOME: 'C:\\Users\\gekcho', // (conceptual equivalent)
PATH: '...', // shortened
TEMP: 'C:\\Users\\gekcho\\AppData\\Local\\Temp',
SystemRoot: 'C:\\Windows'
}
Example 2: This example iterates over the environment variables and display the data in key-values.
// Filename: index.js
// Node.js program to demonstrate the process.env Property
// Include process module
const process = require('process');
// Print all environment variables
let envCount = 0;
// Accessing process.env
const env = process.env;
// Iterating through all returned environment variables
for (const key in env) {
// Print each key-value pair
console.log(`${key}:\t\t\t${env[key]}`);
envCount++;
}
// Printing the total number of environment variables
console.log(`Total number of environment variables available = ${envCount}`);
// Accessing specific environment variables
console.log(`Operating system: ${env['OS']}`);
console.log(`All User Profile: ${env['ALLUSERSPROFILE']}`);
console.log(`Public Directory: ${env['PUBLIC']}`);
Output:
OS: Windows_NT
USERNAME: gekcho
USERPROFILE: C:\Users\gekcho
PATH: C:\Windows\system32;C:\Windows
TEMP: C:\Users\gekcho\AppData\Local\Temp
PUBLIC: C:\Users\Public
Total number of environment variables available = 6
Operating system: Windows_NT
All User Profile: undefined
Public Directory: C:\Users\Public
Example 3: This example demonstrates how to access, modify, and delete environment variables using the process.env property.
// Filename - index.js
// Node.js program to demonstrate the process.env Property
// Include process module
const process = require('process');
// Accessing environment variables
const env = process.env;
console.log("Operating system: " + env.OS);
console.log("All User Profile: " + env.ALLUSERSPROFILE);
console.log("Public Directory: " + env.PUBLIC);
// Setting a new custom environment variable
env.gekcho = "gekcho custom data";
console.log("Stored in env.gekcho: " + env.gekcho);
// Deleting the custom environment variable
delete env.gekcho;
console.log("Stored in env.gekcho after deletion: " + env.gekcho);
Output:
operating system: Windows_NT
alluserprofile: C:\ProgramData
public directory: C:\Users\Public
stored in env.gekcho: gekcho custom data
stored in env.gekcho: undefined
Note: The above program will compile and run by using the node filename.js command.