Environment Variables
Read process environment variables using getEnv from the system module.
javascript
import { getEnv } from "system";Table of Contents
getEnv([name, defaultValue])
Returns either:
- all environment variables as an object (when
nameis omitted), or - a single environment variable value as a string (when
nameis provided).
Parameters
name- Type:
string - Required: No
- Description: Environment variable key (for example,
"PATH").
- Type:
defaultValue- Type:
string - Required: No
- Description: Fallback value used only when
nameis provided and the variable is missing or empty.
- Type:
Return Value
- Type:
object | string - Description:
getEnv()returns an object map of all env vars.getEnv(name)returns a string (empty string when missing).getEnv(name, defaultValue)returnsdefaultValuewhen the variable is missing/empty.
Example: Read One Variable
javascript
import { getEnv } from "system";
const user = getEnv("USERNAME", "unknown");
console.log("user:", user);Example: Read All Variables
javascript
import { getEnv } from "system";
const vars = getEnv();
console.log("PATH:", vars.PATH);
console.log("HOME:", vars.HOME);Notes
- Environment variable names are platform-dependent (Windows commonly uses uppercase keys like
PATH,USERNAME,APPDATA). getEnv(name, defaultValue)treats empty values the same as missing values.