process.env() Method in Node.js

The process.env property in Node.js provides access to environment variables. It returns an object containing all environment variables available to the current process, making it essential for configuration management in Node.js applications.

Syntax

process.env

Note: process.env is a property, not a method, so it doesn't require parentheses.

Parameters

process.env is a read-only object that doesn't accept parameters. It automatically contains all environment variables from the system where the Node.js process is running.

Example: Accessing All Environment Variables

Create a file named env.js and run it using:

node env.js

env.js

// Node.js program to demonstrate the use of process.env

// Printing the user environment object
console.log(process.env);
{
  SHELL: '/bin/bash',
  HOME: '/home/admin',
  USER: 'admin',
  PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  PWD: '/home/admin/test',
  LANG: 'en_US.UTF-8',
  NODE_ENV: 'development',
  ... (many more environment variables)
}

Example: Accessing Specific Environment Variables

// Node.js program to demonstrate accessing specific environment variables

console.log('Home directory:', process.env.HOME);
console.log('Current user:', process.env.USER);
console.log('Node environment:', process.env.NODE_ENV);
console.log('System PATH:', process.env.PATH);

// Accessing a variable that might not exist
console.log('Custom variable:', process.env.MY_CUSTOM_VAR || 'Not set');
Home directory: /home/admin
Current user: admin
Node environment: undefined
System PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Custom variable: Not set

Example: Counting and Iterating Through Environment Variables

// Node.js program to iterate through environment variables

var envCount = 0;
var env = process.env;

// Iterate through all environment variables
for (var key in env) {
    console.log(key + ': ' + env[key]);
    envCount++;
}

console.log('\nTotal environment variables:', envCount);

// Check for common environment variables
console.log('\nCommon variables:');
console.log('Operating System:', process.env.OS || 'Unix-like system');
console.log('Home Directory:', process.env.HOME || process.env.USERPROFILE);
console.log('Username:', process.env.USER || process.env.USERNAME);
SHELL: /bin/bash
HOME: /home/admin
USER: admin
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD: /home/admin/test
LANG: en_US.UTF-8
... (additional variables)

Total environment variables: 45

Common variables:
Operating System: Unix-like system
Home Directory: /home/admin
Username: admin

Common Use Cases

  • Configuration: Store API keys, database URLs, and app settings
  • Environment detection: Distinguish between development, staging, and production
  • System information: Access user directories, system paths, and locale settings
  • Security: Keep sensitive data out of source code

Conclusion

process.env is essential for accessing environment variables in Node.js applications. Use it to manage configuration and keep sensitive information secure outside your codebase.

Updated on: 2026-03-15T23:19:00+05:30

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements