penv
.env loader for Dart

Process Environment Overlay

Part of the Examples. Let real deployment environment variables override values checked into .env.

Source: example/process_environment_example.dart

// Run this example from the package root, optionally overriding API_KEY:
//   dart run example/process_environment_example.dart
//   API_KEY=from-shell dart run example/process_environment_example.dart
//
// Shows how `useProcessEnvironment` lets real deployment environment
// variables (Docker, CI, hosting platforms) take priority over a
// checked-in `.env` file, without pulling in unrelated system variables.

// print() is fine in example scripts; avoid it in production code instead.
// ignore_for_file: avoid_print

import 'dart:io';

import 'package:penv/penv.dart';

const _demoPath = 'example/.env.process';

void main() {
  File(_demoPath).writeAsStringSync('API_KEY=from-dotenv-file\n');

  final env = penvload(
    _demoPath,
    useProcessEnvironment: true, // process env overrides the file by default
  );

  print('API_KEY: ${env['API_KEY']}');
  print(
    '(unrelated system vars like PATH are NOT pulled in): '
    '${env.containsKey('PATH')}',
  );

  File(_demoPath).deleteSync();
}

Run it from the package root:

dart run example/process_environment_example.dart