← Back to React Course | Chapter 14: Performance & Production Deployment | Lesson 9 of 13

React में Environment Variables

Environment variables एक ही recipe के लिए अलग-अलग settings sheets जैसे हैं — एक sheet घर पर practice करने के लिए, और एक अलग sheet असली restaurant में cooking के लिए।
Syntax
markup
# .env file
VITE_VARIABLE_NAME=value

// In component code
const value = import.meta.env.VITE_VARIABLE_NAME;

Environment Variables Define और Read करना

आपके project root में एक .env file key-value pairs define करती है जिन्हें आपका build tool पढ़ता है और आपके code को available कराता है। Vite में, client-exposed variables को VITE_ से prefix होना चाहिए, और इन्हें आपके code में import.meta.env.VITE_SOMETHING के through access किया जाता है।

Note: .env में variables जोड़ने या बदलने के बाद dev server restart करें — ज़्यादातर build tools file को सिर्फ startup पर पढ़ते हैं, live नहीं।
Warning: जिस variable का required prefix (Vite में VITE_) नहीं है वह silently client code को expose नहीं होता, भले ही वह .env file में defined हो।

उदाहरण: Defining and Reading Environment Variables

markup
// Run in your local React project (npm install required)
// .env
VITE_API_URL=https://api.example.com

// In your code:
const apiUrl = import.meta.env.VITE_API_URL;
fetch(apiUrl + '/users');

Client-Side Variables कभी असल में Secret क्यों नहीं होते

कुछ भी जो आपके client-side JavaScript में bundle हो जाता है, इसमें इस तरह exposed environment variables भी शामिल हैं, आखिर में downloadable और readable बन जाता है किसी के लिए भी जो browser devtools खोलकर bundle inspect करे।

असली secrets (write access वाली API keys, database credentials) को हमेशा server-side रहना चाहिए, कभी client-exposed env variable में नहीं।

Note: एक useful test: अगर इस value को publicly leak करना एक problem होगी, तो यह एक VITE_/REACT_APP_-prefixed variable में नहीं होनी चाहिए।
Warning: एक असल में secret API key को एक VITE_-prefixed variable में डालना effectively उस key को उस किसी के लिए publish कर देता है जो आपकी site visit करके devtools खोले।

हर Environment के लिए अलग Values

अलग .env files (जैसे .env.development और .env.production) वही codebase को अपने आप अलग values उपयोग करने देती हैं — जैसे development के दौरान एक local API URL और deploy होने पर असली production API URL — बिना किसी असली code को बदले।

Note: ज़्यादातर build tools current mode (development vs. production) के आधार पर अपने आप सही .env file चुन लेते हैं, बिना extra configuration के।
Warning: गलती से real secrets वाली एक .env.production file public repository में commit करना उन्हें expose कर देता है — sensitive values रखने वाली किसी भी file के लिए .gitignore उपयोग करें।

उदाहरण: Different Values per Environment

markup
// Run in your local React project (npm install required)
// .env.development
VITE_API_URL=http://localhost:4000

// .env.production
VITE_API_URL=https://api.myrealapp.com

// Same code works correctly against both, automatically:
fetch(import.meta.env.VITE_API_URL + '/users');
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. असल में secret values (API secret keys) को एक client-side environment variable में डालना — client JS में bundle हुआ कुछ भी किसी के लिए भी visible है जो devtools खोले।
  2. वह required prefix (जैसे VITE_ या REACT_APP_) भूल जाना जो build tool को असल में एक variable client code को expose करने के लिए चाहिए।
  3. .env file बदलने के बाद dev server restart न करना, update miss करना क्योंकि env vars typically build/start time पर पढ़े जाते हैं।
चैप्टर सारांश
  • Environment variables वही code को development vs. production में अलग तरीके से behave करने देते हैं (जैसे अलग API URLs पर point करना)।
  • Client-exposed variables को browser bundle तक असल में पहुंचने के लिए एक specific prefix (Vite के लिए VITE_, Create React App के लिए REACT_APP_) चाहिए।
  • Client-side code को exposed कोई भी variable publicly visible है — वहां कभी असली secrets न रखें।
  • Variables typically .env files में defined होते हैं, अलग environments के लिए अलग files के साथ (.env.production, .env.development)।
ब्राउज़र सपोर्ट

Build-tool feature (Vite का `import.meta.env` या Create React App का `process.env`); कोई specific React version requirement नहीं।

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.