convar-strict: Environment Validation from .env.example


GitHub npm

Most environment validation libraries require you to define a schema separate from your .env.example file. This creates duplication: you document required variables in .env.example, then redefine them in code with libraries like envalid or t3-env.

convar-strict takes a different approach. It treats .env.example as the source of truth and validates against it automatically.

The Problem

The convar library is simple and convenient, but it fails silently when variables are missing. You only discover the problem when your code tries to use an undefined value.

const convar = require("convar");

const config = {
  database: convar("DATABASE_URL"), // undefined if missing - no error
  apiKey: convar("API_KEY"),        // same problem
};

This violates the principle of failing fast. As discussed in Environment Configuration Best Practices, missing configuration should cause immediate, obvious failures.

The Solution

convar-strict wraps convar and cross-references every variable against .env.example. If a variable is documented but missing from the environment, it throws immediately. If a variable is used but not documented, it warns you.

const convar = require("convar-strict");

const config = {
  database: convar("DATABASE_URL"), // throws if missing
  apiKey: convar("API_KEY"),        // throws if missing
};

With this .env.example:

DATABASE_URL=
API_KEY=
PORT=3000

If DATABASE_URL or API_KEY are missing, you get a clear error at startup:

Missing required environment variables from .env.example: DATABASE_URL, API_KEY

Optional Variables

Comment out variables in .env.example to mark them as optional:

DATABASE_URL=
API_KEY=
# DEBUG=false

DEBUG can now be omitted without triggering an error.

Undocumented Variable Warnings

If your code uses a variable that is not in .env.example, convar-strict warns you. This catches configuration drift where variables get added to deployments but never documented.

Installation

npm install convar-strict

It is a drop-in replacement for convar. Swap the import and your existing code works unchanged, with validation added.

For more on environment configuration patterns, see Environment Configuration Best Practices and Organising Environment Files.