Commit b7158df
committed
feat: Add environment variable validation for development mode
This commit adds a new feature to help developers catch common mistakes with
environment variables during development. The feature automatically scans
source code for process.env.REACT_APP_* references and validates that they
are defined in .env files or the environment.
Key Features:
- Automatically detects all REACT_APP_* environment variable references in
JavaScript and TypeScript files
- Warns developers when they reference undefined environment variables
- Provides intelligent typo detection with suggestions using Levenshtein
distance algorithm (e.g., suggests REACT_APP_API_URL for REACT_APP_API_ULR)
- Only runs in development mode (npm start) - does not affect production builds
- Excludes test files from validation since they often use mocked values
- Can be disabled by setting DISABLE_ENV_CHECK=true
Changes Made:
1. Created checkEnvVariables utility in packages/react-dev-utils/
- Implements fuzzy matching for typo suggestions
- Scans source files using globby
- Provides clear, actionable error messages
2. Integrated validation into npm start workflow
- Added call to checkEnvVariables in packages/react-scripts/scripts/start.js
- Runs after required file checks but before server startup
- Non-blocking - always returns true to avoid breaking builds
3. Added comprehensive test suite
- 11 test cases covering all functionality
- Tests typo detection, multiple variables, TypeScript support, etc.
- All tests passing
4. Updated documentation
- Added Environment Variable Validation section to adding-custom-environment-variables.md
- Updated react-dev-utils README.md with usage examples
- Added feature to package.json files list
Benefits:
- Reduces debugging time by catching environment variable mistakes early
- Improves developer experience with helpful error messages and suggestions
- Prevents production bugs caused by undefined environment variables
- Maintains backward compatibility - optional and non-breaking
This enhancement aligns with Create React App's philosophy of providing a
great developer experience with helpful error messages and automatic
problem detection.1 parent 6254386 commit b7158df
File tree
8 files changed
+515
-23
lines changed- docusaurus/docs
- packages
- create-react-app
- react-dev-utils
- __tests__
- react-scripts/scripts
8 files changed
+515
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
69 | 101 | | |
70 | 102 | | |
71 | 103 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
134 | 156 | | |
135 | 157 | | |
136 | 158 | | |
| |||
0 commit comments