← Back to TypeScript Course | Chapter 13: TypeScript with Node.js | Lesson 6 of 6

Environment Variables की जानकारी

Environment variables ports, API keys, और database URLs जैसी configuration source code के बाहर supply करने देती हैं। Node.js में, वे process.env के through उपलब्ध हैं और possibly undefined strings के रूप में represent होती हैं।
Syntax
typescript
const value: string | undefined = process.env.VARIABLE_NAME;

Environment Variables पढ़ना

Node में process.env के through environment variables पढ़ना string | undefined typed values return करता है, correctly यह reflect करते हुए कि एक expected variable आपके program शुरू होने पर simply set नहीं भी हो सकता।

उदाहरण: Reading Environment Variables

typescript
const apiKey: string | undefined = process.env.API_KEY;
console.log(apiKey);

Environment Variables और Numbers

Environment variables हमेशा strings होती हैं, इसलिए जो भी numeric होना चाहिए — जैसे एक port number — उसे Number() या parseInt से एक explicit conversion चाहिए, क्योंकि process.env.PORT अकेले एक string typed है, कभी number नहीं।

उदाहरण: Environment Variables and Numbers

typescript
const port: number = Number(process.env.PORT) || 3000;
console.log(port);

Required Variables Validate करना

Required variables validate करने का मतलब है यह check करना कि हर एक मौजूद है (और अगर नहीं है तो early throw करना) सीधे startup पर, string | undefined type को बाकी program के safely उपयोग करने के लिए एक guaranteed string में बदलते हुए।

उदाहरण: Validating Required Variables

typescript
function requireEnv(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`Missing env var: ${name}`);
  return value;
}
console.log(typeof requireEnv);

Typed Configuration Objects

Startup पर एक बार बनाया गया एक typed configuration object — हर environment variable को शुरुआत में पढ़ना और validate करना — का मतलब है कि बाकी codebase हर जगह process.env repeatedly check करने के बजाय एक fully-typed config object import कर सकता है।

उदाहरण: Typed Configuration Objects

typescript
interface AppConfig {
  port: number;
  apiKey: string;
}
const config: AppConfig = {
  port: Number(process.env.PORT) || 3000,
  apiKey: process.env.API_KEY || "dev-key",
};
console.log(config);

एक Node Server में Environment Variables

एक Node server में, app.listen() call करने से पहले environment variables validate करना सुनिश्चित करता है कि एक missing critical variable (जैसे एक database URL) बाद में एक request handler के अंदर एक confusing runtime error होने के बजाय boot पर ही fast fail हो।

उदाहरण: Environment Variables in a Node Server

typescript
function requireEnv(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`Missing env var: ${name}`);
  return value;
}
// const dbUrl = requireEnv("DATABASE_URL"); // fails fast at boot
console.log("Validate env vars before app.listen() to fail fast");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि process.env.PORT एक string है और इसे वहां pass करना जहां एक number चाहिए, जब यह string | undefined है।
  2. process.env.PORT को directly एक number के रूप में उपयोग करना, इसलिए PORT + 1 3001 के बजाय "30001" देता है।
  3. यह check न करना कि startup पर एक required variable मौजूद है, इसलिए एक undefined value बाद में काफी देर में errors cause करता है।
चैप्टर सारांश
  • TypeScript को Node के साथ उपयोग करने का मतलब है project set up करना और Node के built-in types के लिए @types/node install करना।
  • File system, HTTP module, और Express का typed उपयोग server code को सुरक्षित बनाता है।
  • Environment variables loosely typed values के रूप में आती हैं, इसलिए उन्हें types के साथ सावधानी से handle किया जाना चाहिए।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.