Skip to main content

Function Signature

async function secretBool(
  key: string,
  fallback?: boolean
): Promise<boolean>

Parameters

key
string
required
The environment variable name that points to the secret file path
fallback
boolean
Optional default value to return if the secret is not found

Returns

Promise<boolean> - The boolean value read from the secret file

How It Works

This function reads secrets from the filesystem, following Docker secrets conventions:
  1. Checks if the environment variable key contains a file path
  2. If no path is set, defaults to /run/secrets/{key}
  3. Reads the file contents and parses it as a boolean
  4. Throws an error if the secret is missing or invalid (unless a fallback is provided)
Boolean values are parsed from strings like “true”, “false”, “1”, “0”, “yes”, “no”, etc.

Example

import { secretBool } from "@nore/load-env"

// Reads from file path in $FEATURE_ENABLED or /run/secrets/FEATURE_ENABLED
export const FEATURE_ENABLED = await secretBool("FEATURE_ENABLED")

// With fallback
export const DEBUG_MODE = await secretBool("DEBUG_MODE", false)

Error Handling

Throws an error if:
  • The secret file doesn’t exist (when no fallback provided)
  • The file is empty (when no fallback provided)
  • The value cannot be parsed as a boolean
When a fallback is provided, returns the fallback value instead of throwing if the file doesn’t exist.

Build docs developers (and LLMs) love