Skip to main content

Function Signature

async function secretInt(
  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 integer to return if the secret is not found

Returns

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

Example

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

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

// With fallback
export const PORT = await secretInt("PORT", 3000)

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