dotenv-oxy
v1.0.4
Published
Type-safe environment variable parser with validation and error handling for Node.js and React Native
Downloads
91
Maintainers
Readme
dotenv-oxy | Node.js Environment Variable Parser
Type-safe environment variable parser with validation and error handling for Node.js and React Native applications.
Features
- Type Safety: Full TypeScript support with proper typing
- Validation: Built-in validation for string, number, boolean, and enum types
- Error Handling: Graceful error handling with descriptive messages
- Lightweight: Only requires
dotenvas a peer dependency - Cross-Platform: Works with Node.js and React Native
Installation
npm install dotenv-oxyAdditional dependencies:
- Node.js:
npm install dotenv - React Native:
npm install react-native-config
Usage
Practical example of using Dotenv-oxy to recover, validate and centralize environmental variables in applications based on Node.JS.
// env.ts
// Node.js
import { getEnvVar } from 'dotenv-oxy';
// React Native
import { getEnvVar } from 'dotenv-oxy/react-native';
// Environment variable declaration
export const env = {
ENVIRONMENT: getEnvVar('ENVIRONMENT', 'enum', ['prod', 'dev']),
DISCORD_TOKEN: getEnvVar('DISCORD_TOKEN', 'string'),
OPENAI_API_KEY: getEnvVar('OPENAI_API_KEY', 'string', false),
PORT: getEnvVar('PORT', 'number', false) || 3000
};// index.ts
import { env } from './env';
env.ENVIRONMENT // 'prod' | 'dev'
env.DISCORD_TOKEN // string
env.OPENAI_API_KEY // string | undefined
env.PORT // numberExplanation
ENVIRONMENTis an string (enum) variable that can only beprodordev, otherwise an error will be thrown at runtime.DISCORD_TOKENis a required string variable, if not set an error will be thrown.OPENAI_API_KEYis an optional string variable, if not set it will returnundefinedwithout throwing an error.PORTis an optional number variable, if not set it will default to3000. If set to a non-numeric value, an error will be thrown.
