Skip to content

Commit b7158df

Browse files
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

8 files changed

+515
-23
lines changed

docusaurus/docs/adding-custom-environment-variables.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ if (process.env.NODE_ENV !== 'production') {
6666

6767
When you compile the app with `npm run build`, the minification step will strip out this condition, and the resulting bundle will be smaller.
6868

69+
## Environment Variable Validation
70+
71+
> Note: this feature is available with `[email protected]` and higher.
72+
73+
When you run `npm start` in development mode, Create React App will automatically scan your source code for references to `process.env.REACT_APP_*` variables and check if they are defined in your `.env` files or environment. If you reference an environment variable that isn't defined, you'll see a helpful warning message:
74+
75+
```
76+
Warning: The following environment variables are referenced in your code but not defined:
77+
78+
REACT_APP_API_URL
79+
Did you mean REACT_APP_API_URI?
80+
81+
To fix this, add the missing variables to your .env file or set them in your environment.
82+
For example, add this line to your .env file:
83+
84+
REACT_APP_API_URL=your_value_here
85+
86+
Learn more: https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
87+
88+
To disable this check, set DISABLE_ENV_CHECK=true in your environment.
89+
```
90+
91+
This validation helps catch common mistakes such as:
92+
93+
- **Typos in variable names** - The validator will suggest similar variable names if it detects a potential typo
94+
- **Forgotten `.env` entries** - Get immediate feedback when you reference a variable that hasn't been defined yet
95+
- **Case sensitivity issues** - Environment variable names are case-sensitive, and the validator helps identify mismatches
96+
97+
The validation only runs during development (`npm start`) and won't affect your production builds. Test files (files ending in `.test.js`, `.test.jsx`, `.test.ts`, `.test.tsx`, or files in `__tests__` directories) are excluded from validation since they often use mocked environment variables.
98+
99+
You can disable this validation by setting `DISABLE_ENV_CHECK=true` in your environment or `.env` file.
100+
69101
## Referencing Environment Variables in the HTML
70102

71103
> Note: this feature is available with `[email protected]` and higher.

package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

packages/react-dev-utils/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,28 @@ if (
131131
}
132132
```
133133

134+
#### `checkEnvVariables(appSrc: string, isInteractive: boolean): boolean`
135+
136+
Scans source code for `process.env.REACT_APP_*` references and validates that they are defined.<br>
137+
Prints helpful warnings for undefined variables, including suggestions for typos.<br>
138+
Only runs in development mode. Returns `true` to indicate non-blocking validation.
139+
140+
```js
141+
var path = require('path');
142+
var checkEnvVariables = require('react-dev-utils/checkEnvVariables');
143+
144+
// Check environment variables before starting dev server
145+
checkEnvVariables(path.resolve('src'), true);
146+
```
147+
148+
Features:
149+
150+
- Detects all `REACT_APP_*` environment variable references in `.js`, `.jsx`, `.ts`, and `.tsx` files
151+
- Warns about undefined variables with helpful error messages
152+
- Suggests corrections for potential typos using fuzzy matching
153+
- Excludes test files from validation
154+
- Can be disabled by setting `DISABLE_ENV_CHECK=true`
155+
134156
#### `clearConsole(): void`
135157

136158
Clears the console, hopefully in a cross-platform way.

0 commit comments

Comments
 (0)