T3 Env
T3 Env is a framework-agnostic, type-safe validation library for environment variables that prevents build-time and runtime configuration errors by utilizing standard schema validators.
T3 Env is a robust, framework-agnostic library designed to manage, validate, and transform environment variables for modern JavaScript and TypeScript applications. It was created to solve the common pain points of missing or incorrectly typed environment variables that often lead to difficult-to-debug runtime errors. By abstracting the validation logic into a centralized, type-safe schema, T3 Env allows developers to maintain clean, predictable configurations across various server-side and client-side runtimes. The library provides a standardized way to define which variables are available in specific environments, ensuring that sensitive server-side secrets are never inadvertently leaked to the client bundle. Because it is built on the Standard Schema specification, it offers unparalleled flexibility in choosing validation engines. Developers can use popular libraries such as Zod, Valibot, ArkType, or Typia to enforce data types, handle default values, and perform custom transformations. By utilizing a Proxy-based implementation for runtime checks, T3 Env provides immediate and descriptive error messages if an undefined or invalid variable is accessed, significantly accelerating the debugging process during development and at build time.
This library effectively centralizes environment management, allowing developers to define schemas that govern the structure and requirements of their environment configuration. By importing a single validated object, applications benefit from full IDE autocompletion and strict TypeScript enforcement. T3 Env handles the complexities of environment injection, providing built-in support for major frameworks like Next.js and Nuxt while maintaining compatibility with general environments like Vite. It addresses common issues associated with tree-shaking and runtime access, ensuring that environment variables are correctly included in the build while remaining inaccessible when they should be. The library also supports advanced features like variable transformation and default value application, which are often difficult to implement correctly when working directly with process.env. By moving these concerns into a purpose-built library, T3 Env simplifies project infrastructure and allows development teams to focus on building features rather than wrestling with runtime configuration errors.
Some of the key features are:
- Type-safe environment variables: Provides full autocompletion and compile-time type checking for all defined environment variables.
- Standard Schema support: Compatible with any validator adhering to the Standard Schema spec, including Zod, Valibot, ArkType, and Typia.
- Server/Client separation: Prevents the accidental exposure of sensitive server-side variables to client-side code through explicit schema definitions.
- Framework agnostic: Works seamlessly with Next.js, Nuxt, Vite, and other common JavaScript and TypeScript frameworks.
- Presets included: Offers ready-to-use configurations for deployment platforms like Vercel, Netlify, and Railway.
- Proxy-based validation: Uses advanced Proxy objects to ensure that accessing invalid or missing variables results in descriptive errors at runtime.
- Flexible transformations: Enables easy coercion of environment strings into booleans, numbers, or other complex types directly within the schema.
To use the tool, developers install the appropriate package for their framework or the core library and define a schema object. This schema maps required environment variables to their corresponding validator logic. By setting the runtimeEnv to point to the system's current environment source (like process.env), the library validates the environment during the build process. Developers then export an env object that provides type-safe, validated access throughout their codebase. If a variable is missing or fails validation, the library throws an error early, preventing the application from starting in an unstable state. This approach ensures that the environment is explicitly defined, documented by the schema, and validated before any business logic is executed.
Some common use cases include:
- Preventing runtime crashes: Validating critical configuration like API keys or database URLs during build time to avoid silent failures at runtime.
- Secure client-side configuration: Safely exposing only necessary public-facing configuration variables to the client browser while keeping server secrets private.
- Data transformation: Converting string-based environment variables from configuration files into usable JavaScript types like numbers, booleans, or parsed objects.
- Managing environment defaults: Providing fallback values for optional environment variables to simplify local development and deployment configurations.
- Ensuring strict type coverage: Improving developer velocity by providing accurate TypeScript definitions for all environment configuration across a large-scale project.
Comments
0Markdown is supported.