TypeScript Application Configuration: Libraries and Approaches

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Configuring TypeScript Applications: Overview of Tools and Approaches



In modern TypeScript development, proper application configuration organization is one of the key tasks. The security, portability, and testability of your code depend on how you manage settings. In this article, we will review the main libraries and tools for configuring TypeScript applications, their features, and usage examples.



Why is Proper Configuration Important?

Configuration includes:

  • Environment variables (API keys, database URLs)
  • Environment parameters (development, staging, production)
  • Framework and library settings
  • Secret data (passwords, tokens)

Using TypeScript adds the benefits of strict typing, which helps avoid errors at compile time. Let's look at the best tools for working with configuration.



1. dotenv — Basic Tool for Environment Variables

The dotenv library loads variables from a .env file into process.env. In TypeScript, it is often used with additional types.



Installation

npm install dotenvnpm install -D @types/node   # for process.env types


Usage Example

// .envPORT=3000DATABASE_URL=mongodb://localhost:27017/mydbSECRET_KEY=mysecretkey

// config.tsimport dotenv from 'dotenv';dotenv.config();

interface AppConfig { port: number; databaseUrl: string; secretKey: string;}

export const config: AppConfig = { port: parseInt(process.env.PORT || '3000', 10), databaseUrl: process.env.DATABASE_URL || '', secretKey: process.env.SECRET_KEY || '',};


Pros: simplicity, lightweight, de facto standard.
Cons: no validation, all values are strings.



2. config — Hierarchical Configuration for Node.js

The config library allows you to organize configuration files by environment (default.json, production.json, development.json).



Installation

npm install confignpm install -D @types/config


Example Structure

// config/default.json{    "server": {        "port": 3000    },    "db": {        "host": "localhost",        "port": 27017    }}

// config/production.json{ "server": { "port": 8080 }, "db": { "host": "production-db.example.com" }}

// app.tsimport config from 'config';

interface ServerConfig { port: number;}

const serverConfig = config.get<ServerConfig>('server');console.log(serverConfig.port); // 3000 in dev, 8080 in production


Pros: hierarchy, automatic environment selection, TypeScript support.
Cons: requires a file system, not suitable for serverless.



3. convict — Configuration with Schema and Validation

convict allows you to define a strict configuration schema with types, default values, and validation.



Installation

npm install convictnpm install -D @types/convict


Example

import convict from 'convict';

const config = convict({ env: { doc: 'Application environment', format: ['production', 'development', 'test'], default: 'development', env: 'NODE_ENV' }, port: { doc: 'Port to bind', format: 'port', default: 3000, env: 'PORT' }, db: { host: { doc: 'Database host', format: String, default: 'localhost', env: 'DB_HOST' }, port: { doc: 'Database port', format: 'port', default: 5432, env: 'DB_PORT' } }

Recommendations