Node.js process.env Property
Last Updated :
15 Oct, 2024
In Node.js, the process.env property is an essential part of the runtime environment that provides access to environment variables. These variables are key-value pairs that can influence the behaviour of your Node.js application, making it easier to configure and manage different settings across development, testing, and production environments.
What is process.env in Node.js?
The process.env property is a global object in Node.js that stores environment variables. These variables can be accessed by any application running in that environment, not just Node.js applications. When a Node.js application runs, it inherits the environment variables from the operating system and can use them to dynamically configure the application, such as database connections, API keys, or application modes (development or production).
Syntax:
console.log(process.env); // Outputs all environment variables
console.log(process.env.NODE_ENV); // Outputs the value of NODE_ENV environment variable
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.
Explore this Process Module Complete Reference to discover detailed explanations, advanced usage examples, and expert tips for mastering its powerful features, helping you effectively manage, control, and monitor your Node.js applications for improved performance, scalability, and troubleshooting.
Why Use Environment Variables?
Environment variables offer several advantages, including:
- Separation of Concerns: Environment variables allow you to separate application logic from configuration. This makes your code cleaner, more modular, and easier to maintain.
- Security: Sensitive information, such as database credentials or API keys, should never be hardcoded in your application code.
- Portability: By using environment variables, you can easily run your application across different environments (development, staging, production) without changing the code.
- Version Control Safety: Since environment variables are external to your codebase, they don’t need to be included in version control (e.g., Git).
Setting Environment Variables in Node.js
Environment variables can be set in various ways, depending on the platform you are working on.
On Unix/Linux/macOS: You can set environment variables inline when starting your Node.js application:
NODE_ENV=production PORT=3000 node app.js
In this example:
- NODE_ENV=production sets the NODE_ENV environment variable to production.
- PORT=3000 sets the PORT environment variable to 3000.
On Windows: On Windows, you can set environment variables using the set command:
set NODE_ENV=production && node app.js
Using process.env property in Node.js
Example 1: This example console log the process.env object.
JavaScript
// 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:
{
ALLUSERSPROFILE: 'C:\\ProgramData',
APPDATA: 'C:\\Users\\gekcho\\AppData\\Roaming',
cmake: 'D:\\programfiles\\Cmake\\bin\\cmake.exe',
CommonProgramFiles: 'C:\\Program Files\\Common Files',
'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
CommonProgramW6432: 'C:\\Program Files\\Common Files',
COMPUTERNAME: 'gekchos_lappy',
ComSpec: 'C:\\Windows\\system32\\cmd.exe',
DriverData: 'C:\\Windows\\System32\\Drivers\\DriverData',
GTK_BASEPATH: 'C:\\Program Files (x86)\\GtkSharp\\2.12\\',
HADOOP_HOME:
'C:\\Users\\gekcho\\Downloads\\Compressed\\hadoop-3.1.0\\hadoop-3.1.0\\bin',
HOMEDRIVE: 'C:',
HOMEPATH: '\\Users\\gekcho',
JAVA_HOME: 'C:\\Java\\jdk1.8.0_201',
LOCALAPPDATA: 'C:\\Users\\gekcho\\AppData\\Local',
LOGONSERVER: '\\\\gekchos_lappy',
MAGICK_HOME: 'C:\\wamp64\\bin\\php\\php7.3.1\\ext\\ImageMagick',
NUMBER_OF_PROCESSORS: '4',
OneDrive: 'C:\\Users\\gekcho\\OneDrive',
OneDriveConsumer: 'C:\\Users\\gekcho\\OneDrive',
OS: 'Windows_NT',
Path:
'C:\\wamp64\\bin\\php\\php7.3.1\\ext\\ImageMagick;
C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;
C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;
C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;
C:\\Windows\\System32\\OpenSSH\\;D:\\programfiles\\Git\\cmd;
D:\\programfiles\\Cmake\\bin;C:\\Program Files\\nodejs\\;
C:\\Users\\gekcho\\Downloads\\Compressed\\hadoop-3.1.0\\hadoop-3.1.0\\bin;
C:\\Java\\jdk1.8.0_201\\bin;
C:\\Users\\gekcho\\Downloads\\Compressed\\spark-2.4.4-bin-hadoop2.7\\bin;
C:\\Program Files (x86)\\GtkSharp\\2.12\\bin;
C:\\Users\\gekcho\\AppData\\Local\\Microsoft\\WindowsApps;
C:\\Users\\gekcho\\AppData\\Roaming\\npm',
PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
PROCESSOR_ARCHITECTURE: 'AMD64',
PROCESSOR_IDENTIFIER: 'Intel64 Family 6 Model 142 Stepping 9, GenuineIntel',
PROCESSOR_LEVEL: '6',
PROCESSOR_REVISION: '8e09',
ProgramData: 'C:\\ProgramData',
ProgramFiles: 'C:\\Program Files',
'ProgramFiles(x86)': 'C:\\Program Files (x86)',
ProgramW6432: 'C:\\Program Files',
PROMPT: '$P$G',
PSModulePath:
'C:\\Program Files\\WindowsPowerShell\\Modules;
C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules',
PUBLIC: 'C:\\Users\\Public',
python3:
'C:\\Users\\gekcho\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe',
SESSIONNAME: 'Console',
SPARK_HOME:
'C:\\Users\\gekcho\\Downloads\\Compressed\\spark-2.4.4-bin-hadoop2.7',
SystemDrive: 'C:',
SystemRoot: 'C:\\Windows',
TEMP: 'C:\\Users\\gekcho\\AppData\\Local\\Temp',
TMP: 'C:\\Users\\gekcho\\AppData\\Local\\Temp',
USERDOMAIN: 'gekchos_lappy',
USERDOMAIN_ROAMINGPROFILE: 'gekchos_lappy',
USERNAME: 'gekcho',
USERPROFILE: 'C:\\Users\\gekcho',
windir: 'C:\\Windows'
}
Example 2: This example iterates over the environment variables and display the data in key-values.
JavaScript
// 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:
ALLUSERSPROFILE: C:\ProgramData
APPDATA: C:\Users\gekcho\AppData\Roaming
cmake: D:\programfiles\Cmake\bin\cmake.exe
CommonProgramFiles: C:\Program Files\Common Files
CommonProgramFiles(x86):C:\Program Files (x86)\Common Files
CommonProgramW6432: C:\Program Files\Common Files
COMPUTERNAME: gekchos_lappy
ComSpec: C:\Windows\system32\cmd.exe
DriverData: C:\Windows\System32\Drivers\DriverData
GTK_BASEPATH: C:\Program Files (x86)\GtkSharp\2.12\
HADOOP_HOME: C:\Users\gekcho\Downloads\Compressed\hadoop-3.1.0\hadoop-3.1.0\bin
HOMEDRIVE: C:
HOMEPATH: \Users\gekcho
JAVA_HOME: C:\Java\jdk1.8.0_201
LOCALAPPDATA: C:\Users\gekcho\AppData\Local
LOGONSERVER: \\gekchos_lappy
MAGICK_HOME: C:\wamp64\bin\php\php7.3.1\ext\ImageMagick
NUMBER_OF_PROCESSORS: 4
OneDrive: C:\Users\gekcho\OneDrive
OneDriveConsumer: C:\Users\gekcho\OneDrive
OS: Windows_NT
Path: C:\wamp64\bin\php\php7.3.1\ext\ImageMagick;
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;
C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Windows\System32\OpenSSH\;D:\programfiles\Git\cmd;
D:\programfiles\Cmake\bin;C:\Program Files\nodejs\;
C:\Users\gekcho\Downloads\Compressed\hadoop-3.1.0\hadoop-3.1.0\bin;
C:\Java\jdk1.8.0_201\bin;
C:\Users\gekcho\Downloads\Compressed\spark-2.4.4-bin-hadoop2.7\bin;
C:\Program Files (x86)\GtkSharp\2.12\bin;
C:\Users\gekcho\AppData\Local\Microsoft\WindowsApps;
C:\Users\gekcho\AppData\Roaming\npm
PATHEXT: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE:AMD64
PROCESSOR_IDENTIFIER: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
PROCESSOR_LEVEL: 6
PROCESSOR_REVISION: 8e09
ProgramData: C:\ProgramData
ProgramFiles: C:\Program Files
ProgramFiles(x86): C:\Program Files (x86)
ProgramW6432: C:\Program Files
PROMPT: $P$G
PSModulePath: C:\Program Files\WindowsPowerShell\Modules;
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
PUBLIC: C:\Users\Public
python3: C:\Users\gekcho\AppData\Local\Programs\Python\Python37-32\python.exe
SESSIONNAME: Console
SPARK_HOME: C:\Users\gekcho\Downloads\Compressed\spark-2.4.4-bin-hadoop2.7
SystemDrive: C:
SystemRoot: C:\Windows
TEMP: C:\Users\gekcho\AppData\Local\Temp
TMP: C:\Users\gekcho\AppData\Local\Temp
USERDOMAIN: gekchos_lappy
USERDOMAIN_ROAMINGPROFILE:gekchos_lappy
USERNAME: gekcho
USERPROFILE: C:\Users\gekcho
windir: C:\Windows
total no of values available = 46
operating system: Windows_NT
alluserprofile: C:\ProgramData
public directory: C:\Users\Public
Example 3: This example demonstrates how to access, modify, and delete environment variables using the process.env property.
JavaScript
// 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.