Function Signature
Parameters
The environment variable name that points to the secret file path
Optional default string to return if the secret is not found
Returns
Promise<string> - The string value read from the secret file
How It Works
This function reads secrets from the filesystem, following Docker secrets conventions:- Checks if the environment variable
keycontains a file path - If no path is set, defaults to
/run/secrets/{key} - Reads the file contents as a UTF-8 string (trimmed)
- Throws an error if the secret is missing or empty (unless a fallback is provided)
Example
Docker Secrets Usage
/run/secrets/API_KEY in the container.
Error Handling
Throws an error if:- The secret file doesn’t exist (when no fallback provided)
- The file is empty (when no fallback provided)
