Skip to content

Environment Variables

Read process environment variables.

javascript
import { getEnv } from "system";

Availability

Available in the Main script only.

Table of Contents

Methods

getEnv([name [, defaultValue]]) #
system

Returns environment variable data depending on how it is called:

  • getEnv() — returns an object containing all environment variables as key/value pairs
  • getEnv(name) — returns the value of the named variable, or an empty string if missing or empty
  • getEnv(name, defaultValue) — returns the variable value, or defaultValue if the variable is missing or empty

Windows Environment Variables

On Windows, environment variable names are case-insensitive but typically stored in uppercase (PATH, USERNAME, APPDATA). Empty values are treated the same as missing when a defaultValue is provided.

PARAMETERS

name string OPTIONAL
Environment variable key (e.g. PATH, USERNAME). If omitted, all variables are returned.
defaultValue string OPTIONAL
Fallback value returned when the variable is missing or empty. Only used when name is provided.
RETURNS: object | string An object map of all env vars when called with no arguments, or a string value for a specific variable.
javascript
import { getEnv } from "system";

// Single variable with fallback
const user = getEnv("USERNAME", "unknown");
console.log("User:", user);

// Single variable, no fallback
const appdata = getEnv("APPDATA");
console.log("AppData:", appdata);

// All variables
const all = getEnv();
console.log("PATH:", all.PATH);