AbsoluteJS

Environment Variables

Safe environment variable access with getEnv from @absolutejs/absolute.

#Accessing Environment Variables

Use getEnv to read environment variables from your .env file. It throws an error if the variable is missing, catching configuration errors at startup:

TS
import { getEnv } from '@absolutejs/absolute';

// getEnv reads from .env and throws if the variable is missing
const databaseUrl = getEnv('DATABASE_URL');
const callbackUri = getEnv('OAUTH2_CALLBACK_URI');

// Use in your server configuration
const db = drizzle(databaseUrl);

#Required Variables

Common environment variables for AbsoluteJS applications:

BASH
# .env file
DATABASE_URL=postgresql://user:pass@localhost:5432/db
HOST=localhost
PORT=3000

# For OAuth authentication:
OAUTH2_CALLBACK_URI=http://localhost:3000/auth/callback

# Provider-specific credentials:
GITHUB_CLIENT_ID=...
GITHUB_CLIENT_SECRET=...
DATABASE_URLConnection string for your database
HOSTServer host (default: localhost)
PORTServer port (default: 3000)
OAUTH2_CALLBACK_URICallback URL for OAuth providers (e.g., http://localhost:3000/auth/callback)

#Fail-Fast Validation

getEnv validates environment variables at startup. If a variable is missing, your server fails immediately with a clear error message instead of crashing later at runtime:

TS
import { getEnv } from '@absolutejs/absolute';

// getEnv throws immediately if the variable is not set
// This catches configuration errors at startup, not runtime

const config = {
  database: getEnv('DATABASE_URL'),
  host: getEnv('HOST'),
  port: getEnv('PORT'),
  callbackUri: getEnv('OAUTH2_CALLBACK_URI')
};

// If any variable is missing, you get a clear error:
// Error: Missing environment variable DATABASE_URL

// All variables are guaranteed to be strings (never undefined)
const db = drizzle(config.database);