feat: Add environment variable validation for development mode #17167
+515
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR 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.envfiles or the environment.Motivation
Currently, when developers reference undefined environment variables in Create React App, they receive no warning until runtime, leading to subtle bugs where
process.env.REACT_APP_SOMETHINGisundefined. This feature provides immediate feedback during development to catch these issues early.Key Features
REACT_APP_API_URLforREACT_APP_API_ULR)npm start) - does not affect production buildsDISABLE_ENV_CHECK=trueExample Output
When a developer references undefined variables:
Changes Made
1. Created
checkEnvVariablesutility inpackages/react-dev-utils/2. Integrated validation into npm start workflow
checkEnvVariablesinpackages/react-scripts/scripts/start.js3. Added comprehensive test suite
4. Updated documentation
adding-custom-environment-variables.mdreact-dev-utils/README.mdwith API documentation and usage examplesTesting
All tests pass:
End-to-end testing confirmed:
DISABLE_ENV_CHECK=trueBenefits
Backward Compatibility
This feature is:
DISABLE_ENV_CHECK=trueChecklist
Related Issues
This addresses a common pain point mentioned in various issues where developers struggle to debug undefined environment variables. The feature provides immediate, actionable feedback similar to other CRA developer experience improvements.