Reading Configuration

Access and retrieve configuration values in your application using standard patterns and best practices.

🔍

Reading Config

Reading Configuration Values

Use standard methods to access configuration values across different parts of your application. These patterns ensure you can dynamically respond to environment-specific settings.

Node.js

const configValue = process.env.MY_APP_SETTING;
console.log('Current value:', configValue);

Use process.env to access environment variables in Node.js applications.

Client-Side JS

const mySetting = import.meta.env.VITE_MY_SETTING;
console.log('Current value:', mySetting);

Access special environment variables in Vite-based projects.

Access Patterns

Dynamic Access

const apiBase = import.meta.env.VITE_API_URL || 'https://api.defaults'

Provide fallback values for when configuration is missing.

Validation

const apiTimeout = Number(process.env.API_TIMEOUT) || 5000;

Always validate and cast type when reading values.

Security Considerations

Never Expose Secrets

Never output sensitive values in client-side code or logs.

// ❌ Bad Practice
console.log('Secret value:', process.env.SECRET_KEY);

Safe Usage

Use server-side endpoints for sensitive operations.

// ✔ Safe Method
fetch('/api/secure', {
  headers: { 'X-ApiKey': import.meta.env.Saftey_KEY }

Related Documentation