Skip to main content

Function Signature

async function secretFloat(
  key: string,
  fallback?: number
): Promise<number>

Parameters

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

Returns

Promise<number> - The floating-point number 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 float using parseFloat()
  4. Throws an error if the secret is missing or invalid (unless a fallback is provided)

Example

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

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

// With fallback
export const TAX_RATE = await secretFloat("TAX_RATE", 0.08)

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 number (NaN)
When a fallback is provided, returns the fallback value instead of throwing if the file doesn’t exist.

Build docs developers (and LLMs) love