Environment Variables

Manage environment variables professionally in your Thunder application using the built-in Env utility.

When developing any application, it is an excellent practice to manage environment variables in a clean and consistent manner. Thunder provides a first-class Env utility class that makes reading, listing, and checking environment variables straightforward - with both synchronous and asynchronous support out of the box.

Setting Up Environment Variables

Create a .env file at the root of your project. This file serves as the global environment configuration for your Thunder application:

.env
APP_NAME=Thunder
PORT=3742
DATABASE_URL=mongodb://localhost:27017/thunder
JWT_SECRET=your_super_secret_key

The .env file is automatically loaded by Thunder at startup. You do not need any additional configuration to enable this behaviour.

Never commit your .env file to version control. Add it to your .gitignore immediately to prevent accidental exposure of sensitive credentials.

Reading Environment Variables

Thunder exposes the Env utility class to access your environment variables from anywhere in your application. Both synchronous and asynchronous methods are supported.

Env.get(key) - Async

import { Env } from "@/utils/env.ts";

const dbUrl = await Env.get("DATABASE_URL");
console.log(dbUrl); // "mongodb://localhost:27017/thunder"

Env.getSync(key) - Sync

import { Env } from "@/utils/env.ts";

const appName = Env.getSync("APP_NAME");
console.log(appName); // "Thunder"

Additional utility methods are available in utils/env.ts. You are encouraged to explore that file for the full API surface of the Env class.


On this page