Skip to main content

Function Signature

async function secretDate(
  key: string,
  fallback?: Date
): Promise<Date>

Parameters

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

Returns

Promise<Date> - The Date object parsed 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 Date using the Date constructor
  4. Throws an error if the secret is missing or invalid (unless a fallback is provided)
The date string should be in any format accepted by JavaScript’s Date constructor (ISO 8601, etc.).

Example

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

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

// With fallback
export const START_DATE = await secretDate("START_DATE", new Date("2024-01-01"))

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

Build docs developers (and LLMs) love