📦 SETUP • ⚙️ CONFIGURATION • 🛰️ FEATURES
| A lightweight, type-safe environment variable validator for TypeScript and Bun, powered by Zod. Features auto-loading, variable expansion, and automatic .env.example generation |
bun i -D envlysnpm i -D envlyspnpm i -D envlysyarn i -D envlysIn order to validate your environment variables, you can use the createEnvironment function from the envlys package. This function takes an object with the following properties:
import { createEnvironment } from 'envlys';
import { z } from 'zod';
const environment = z.object({
NODE_ENV: z.enum([ 'development', 'production'], {
error: "NODE_ENV has to be either 'development' or 'production'"
}),
PORT: z.coerce.number({ error: 'PORT must be a number (e.g., 4321)' }),
BASE_URL: z.string({
error: 'Base URL is required and must be a string (e.g., http://localhost:4321)'
}).default('http://localhost:4321')
});
const { NODE_ENV, PORT, BASE_URL } = createEnvironment(environment, {
path: './',
generateExample: true,
listEnvironment: [
'development',
'production'
]
});The createEnvironment function accepts a second parameter, which is an object with the following optional properties:
path: A string that specifies the path to the directory where the.envfile is located. Default is./.generateExample: A boolean that indicates whether to generate a.env.examplefile based on the provided Zod schema. Default isfalse.listEnvironment: An array of strings that specifies the valid values for theNODE_ENVvariable. Default is['development', 'production']this help to generate examples for every environment.filename: A string that specifies the name of the.envfile to load. Default is.env.
BACK TO TOP
Copyright © All rights reserved, developed by LuisdaByte and