@natoboram/load_env

@natoboram/load_env

Node.js CI GitHub Pages Dependabot Updates GitHub Downloads NPM Downloads

A standalone implementation of Vite's loadEnv.

@natoboram/load_env is available on npmjs, GitHub Packages, and GitHub Releases.

pnpm i @natoboram/load_env

The loadEnv function loads environment variables from the current directory's .env files. NODE_ENV has to be set in the environment and will not be picked up from the filesystem.

If NODE_ENV is not set, it defaults to development.

Environment variables are loaded in the following order:

  1. .env.${NODE_ENV}.local
  2. .env.${NODE_ENV}
  3. .env.local
  4. .env

Additional functions are provided for the type safety of environment variables. See the documentation for the full list of available functions.

import {
envBool,
envFloat,
envInt,
envString,
envUrl,
envUuid,
loadEnv,
} from "@natoboram/load_env"
import type { UUID } from "crypto"

loadEnv()

export const API_URL: URL = envUrl("API_URL")
export const ENABLE_TELEMETRY: boolean = envBool("ENABLE_TELEMETRY")
export const OFFSET: number = envFloat("OFFSET")
export const PORT: number = envInt("PORT")
export const TOKEN: UUID = envUuid("TOKEN")
export const USERNAME: string = envString("USERNAME")

Note that envUuid doesn't actually check if the string is a valid UUID.