Back to all snippets
Library/Node.js/Environment Config
typescriptintermediateconfigenvironmentvalidation

How to implement Environment Config in Typescript

Type-safe environment variable configuration

Quick Answer

Centralise all `process.env` access into a single typed config object so missing or wrong-type variables are caught at startup, not deep in runtime code.

Code Snippet

1interface Env {
2  NODE_ENV: 'development' | 'production' | 'test';
3  PORT: number;
4  DATABASE_URL: string;
5}
6
7function getEnv(): Env {
8  const env = process.env;
9  
10  return {
11    NODE_ENV: (env.NODE_ENV as Env['NODE_ENV']) || 'development',
12    PORT: parseInt(env.PORT || '3000', 10),
13    DATABASE_URL: env.DATABASE_URL || ''
14  };
15}
16
17export const config = getEnv();

What is Environment Config?

Scattering process.env access throughout a codebase makes it hard to track which variables are required and what types they should be. This pattern centralises all environment variable reading into a single config object with type coercions, making missing variables obvious and providing a single source of truth for all configuration.

How It Works

  1. 1Define an `Env` interface with the exact shape and types you need.
  2. 2A `getEnv()` factory reads `process.env`, applies type coercions, and returns a typed object.
  3. 3Export a single `config` constant — everywhere else imports from this file, never from `process.env` directly.
  4. 4Validate required fields early in `getEnv()` and throw a descriptive error if they are missing.

Common Use Cases

  • API services - Centralise database URL, port, and secret key config
  • Multi-environment apps - Validate required variables are present before startup
  • Docker deployments - Make environment requirements explicit for ops teams
  • CI/CD pipelines - Fail fast with clear errors when required vars are missing

Key Benefits

  • Single file to audit all required environment variables
  • Type coercions convert string env vars to numbers and booleans
  • Makes undefined required vars obvious at startup rather than at runtime
  • Fully typed config object improves IDE autocomplete

Common Mistakes to Avoid

  • Calling `process.env.VARIABLE` directly outside of the config file — this bypasses your type safety.
  • Not validating required variables — if DATABASE_URL is undefined the app silently uses an empty string and fails later.
  • Parsing numbers without `parseInt` — `process.env` always returns strings, so `port + 1` returns `'30001'` not `3001`.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more typescript examples

Frequently Asked Questions

Should I use a library like zod or envalid for env validation?

Yes — for production apps, a library like `zod` or `envalid` gives you schema-level validation with clear error messages and supports `.env` file loading. This snippet is a lightweight alternative for simple projects.

About This Typescript Code Snippet

This free typescript code snippet for environment config is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this intermediate-level example will help you implement environment config quickly and correctly.

All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.

Tags: config, environment, validation  | Language: typescript  | Difficulty: intermediate  | Category: Node.js

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.