Skip to content

Getting Started

Abhijeet Kumar edited this page Nov 20, 2026 · 1 revision

Getting Started

Install

npm install env-saaf

env-saaf has zero dependencies and works in plain CommonJS Node.js projects. TypeScript definitions are included.

Your first schema

Describe every variable your app needs, in one place:

const { validateEnv } = require('env-saaf');

const env = validateEnv({
  PORT:      { type: 'port', required: true },
  DEBUG:     { type: 'boolean', default: false },
  API_URL:   { type: 'url', required: true },
  LOG_LEVEL: { type: 'string', choices: ['info', 'warn', 'error'], default: 'info' },
});

If anything is missing or invalid, validateEnv throws one EnvSaafError listing all problems at once — at startup, not on the first production request.

Loading a .env file

const { loadEnv } = require('env-saaf');

const env = loadEnv({
  PORT: { type: 'port', required: true },
});

loadEnv reads .env from the working directory (or the paths you pass), supports ${VAR} interpolation and ${VAR:-default} fallbacks, multiline quoted values, and can load several files in order.

Non-throwing mode

When you want the list of problems instead of an exception:

const { env, errors } = validateEnv(schema, process.env, { throwOnError: false });

env holds every value that validated; errors holds the messages.

Next: CLI Guide · Types Reference

Clone this wiki locally