Config is a smart, enhanced, and framework-agnostic configuration setup designed for TypeScript backend and frontend projects. It eliminates configuration boilerplate, providing auto type-safety for your environment variables with minimal setup.
Just copy, paste, and run—it's that simple!
Config is designed to save developers time and effort by consolidating configuration steps:
-
Forget maintaining separate Interfaces, Enums, validation classes, and
.env.examplefiles. Config achieves type-safety by relying solely on a Zod schema definition.- Before: Multiple files were required to ensure a variable (like
PORT) was correctly typed, validated, and documented. - Now: Define your variables once in
env.schema.tsusing Zod, and you gain guaranteed type-safety whenever you retrieve a variable usingConfigService.
// env.schema.ts (Minimal setup) export function envSchema() { return z.object({ PORT: z.preprocess((val) => Number(val), z.number().int().positive()), // Automatically ensures PORT is a number DB_URL: z.string().nonempty(), }); } // Usage: index.ts const config = new ConfigService(); // 'databaseUrl' is automatically inferred as a string by TypeScript const databaseUrl = config.get((env) => env.DATABASE_URL);
- Before: Multiple files were required to ensure a variable (like
-
Config isn't tied to any specific framework. Use it in NestJS, plain Node/Express, or even in a Frontend application with little to no modification.
The Config service supports managing multiple environments, simplifying testing and deployment across different stages.
How it works:
You can maintain separate configuration files (e.g., .env.development, .env.test, etc.) in the root of your project. The system determines which file to load based on the value of the standard NODE_ENV variable.
To switch environments, you simply adjust the NODE_ENV value in your primary .env file:
# .env (Note: Environment files should typically be excluded from version control.)
# ➡️ To use configurations from .env.development
NODE_ENV=development
# ➡️ To switch to configurations from .env.test
# NODE_ENV=test
This mechanism allows you to effortlessly toggle between development, testing, and production configurations.
However, if you prefer to have all your env variables in one file, you can too, and it will work just fine.
-
The
ConfigServicecan be instantiated directly, making it ideal for use in places where Dependency Injection (like in NestJS) isn't available, such as database configuration files or utility scripts.
This project is designed for quick adoption by simply copying the configuration files into your existing project.
- Node.js
- TypeScript
-
Copy Files: Copy the entire
src/configfolder into your project'ssrcdirectory.Tip: Ensure you have the necessary dependencies for the underlying schema validation (e.g., Zod) installed in your project:
# Install necessary dependencies (adjust based on your actual schema implementation) npm install zod # or pnpm install zod
-
Define Schema: Customize your environment variables and their types in the schema file:
env.schema.ts -
Setup
.env: Create your environment file (e.g.,.env) in the project root to define the variables specified in your schema.
Once the config folder is copied and the schema is defined, you can import and use the ConfigService anywhere in your application:
import { ConfigService } from './config/config.service';
// 1. Instantiate the service
const config = new ConfigService();
// 2. Retrieve variables with guaranteed type-safety
// TypeScript knows 'databaseUrl' is a string.
const databaseUrl = config.get((env) => env.DATABASE_URL);
console.log('Database URL:', databaseUrl);
// Example Output -> Database URL: postgres://dev_user:dev_pass@localhost:5432/my_app_dev- Justice Nkweke - Justice's GitHub Profile