env-aligner
v2.0.0
Published
Validate, align, and clone env files (e.g. .env) using a schema file (e.g. .env.example).
Downloads
19
Readme
Env Aligner
Env Aligner is a lightweight CLI tool that helps you validate and align environment variables between .env and .env.example files.
It checks for missing keys, empty values, and extra variables, and optionally formats .env files to match the schema.
Here's what
env-aligneroutput looks like:
Installation
npm install env-alignerFeatures
Env Aligner recursively searches your project directories and provides the following features:
- Missing Key: Detects keys defined in the schema file (e.g.,
.env.example) but missing from the.envfile. - Missing Value: Detects variables that exist in
.envbut have no assigned value. - Extra Key: Detects keys in
.envthat are not defined in the schema file. This is only checked in strict mode. - Align: Automatically aligns the order and structure of the
.envfile based on the schema. This feature requires strict mode to be enabled. - Clone: Creates a new
.envfile based on the schema file if none exists.
Usage
CLI Options
| Option | Description | Default |
|---------------|-----------------------------------------------------------------------------|-------------------|
| --dir | Root directory to scan. | process.cwd() |
| --schema | Schema file name (usually .env.example). | .env.example |
| --env | Env file name to validate or align. | .env |
| --strict | Enable strict mode: warns about extra keys in .env not in the schema. | Not enabled (unless flag is present) |
| --align | Enable align mode: auto-fix .env format based on schema. ⚠️ Only works with --strict. | Not enabled (unless flag is present) |
| --clone | Clone schema to env file if env does not exist in the folder. | Not enabled (unless flag is present) |
Examples:
npx env-aligner
npx env-aligner --dir .devcontainer --schema config.example --env config.env
npx env-aligner --clone
npx env-aligner --strict --alignProgrammatic Usage (Optional)
Although Env Aligner is primarily designed for use via the CLI, it also supports programmatic usage in JavaScript environments.
⚠️ Note: Programmatic usage is only supported in CommonJS (require) environments.
If your project uses "type": "module" (ESM), consider using the CLI instead.
const envAligner = require('env-aligner')
envAligner({
rootDir: string, // Root directory to scan (default: process.cwd())
fileNames: {
schemaName: string, // Schema file name (e.g., '.env.example')
envName: string // Env file name to validate (e.g., '.env')
},
mode: {
isStrict: boolean, // Enable strict mode (extra key detection)
isAlign: boolean // Enable align mode (auto format fix)
},
isClone: boolean // Whether to generate the env file if missing
})Changelog
See CHANGELOG.md for full version history.
License
MIT © 2025 Jeremy Ho & MJC
Got suggestions or questions? Open an issue or drop us a message — we’d love to hear from you!

