@deyvan/envdir
v1.0.3
Published
Loads environment variables from files in a specified directory. Each file becomes a variable where its name is the key and its content is the value.
Maintainers
Readme
@deyvan/envdir
A utility that parses a specified directory and loads its contents into process.env. Perfect for use in Docker containers or environments where it's common to provide configuration via files (e.g., Docker Secrets, Kubernetes Secrets, or AWS ECS secrets).
How it works
- You provide a path to a directory (e.g., /run/secrets).
- The package reads all files within that directory.
- For each file, it creates an environment variable: Filename (e.g., DATABASE_URL) becomes the variable name (process.env.DATABASE_URL). File content (e.g., postgres://user:pass@host:5432/db) becomes the variable value.
Example
Directory structure:
/run/secrets/
├── DATABASE_URL
├── API_KEY
└── REDIS_URLAfter using the package:
import loadEnvDir from '@deyvan/envdir'
loadEnvDir("/run/secrets") // loads ./env/ by default
console.log(process.env.DATABASE_URL) // "postgres://..."
console.log(process.env.API_KEY) // "your-super-secret-api-key"
console.log(process.env.REDIS_URL) // "redis://127.0.0.1:16379"Also without env injection:
import loadEnvDir from '@deyvan/envdir'
const env = loadEnvDir("/run/secrets", {dontInjectEnv: true}) // loads ./env/ by default
console.log(env.DATABASE_URL) // "postgres://..."
console.log(env.API_KEY) // "your-super-secret-api-key"
console.log(env.REDIS_URL) // "redis://127.0.0.1:16379"