Skip to content

TheElegantCoding/envlys

Repository files navigation

Envlys


divider

  📦 SETUP⚙️ CONFIGURATION🛰️ FEATURES

divider


               

divider

About

A lightweight, type-safe environment variable validator for TypeScript and Bun, powered by Zod. Features auto-loading, variable expansion, and automatic .env.example generation

divider

Table of content


divider

Requirements

  • node >= 22.17.0
  • bun >= 1.1.0

divider

Installation

Bun

bun i -D envlys

Npm

npm i -D envlys

Pnpm

pnpm i -D envlys

Yarn

yarn i -D envlys

divider

Usage

In 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'
  ]
});

divider

Configuration

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 .env file is located. Default is ./.
  • generateExample: A boolean that indicates whether to generate a .env.example file based on the provided Zod schema. Default is false.
  • listEnvironment: An array of strings that specifies the valid values for the NODE_ENV variable. Default is ['development', 'production'] this help to generate examples for every environment.
  • filename: A string that specifies the name of the .env file to load. Default is .env.

divider

  BACK TO TOP

divider

  Copyright © All rights reserved,
  developed by LuisdaByte and

About

📦️ A lightweight, type-safe environment variable validator for TypeScript and Bun, powered by Zod. Features auto-loading, variable expansion, and automatic .env.example generation

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors