Express Example
This example shows how to use dotenv-gad in an Express application to validate environment variables and fail fast if required configuration is missing.
Schema (env.schema.ts):
import { defineSchema } from 'dotenv-gad';
export default defineSchema({
PORT: { type: 'number', default: 3000, docs: 'Port to run the server on' },
DATABASE_URL: { type: 'string', required: true, sensitive: true },
});
Server (server.ts):
import express from 'express';
import schema from './env.schema';
import { loadEnv } from 'dotenv-gad';
const env = loadEnv(schema); // throws on validation errors
const app = express();
app.get('/', (req, res) => res.send('Hello'));
app.listen(env.PORT, () => console.log(`Server running on ${env.PORT}`));
Notes
loadEnvreads from bothprocess.envand your.envfile. On platforms like Vercel, Railway, or Docker where env vars are injected directly, it just works — no.envfile needed.- Use
includeRawduring local debugging if you want raw values to appear in errors:loadEnv(schema, { includeRaw: true }). - For production, keep
includeRawdisabled to avoid leaking values in logs.
Try it online
- StackBlitz: https://stackblitz.com/github/kasimlyee/dotenv-gad/tree/main/examples/express
- Run locally:
cd examples/express
npm ci
npm start