feat: Add environment variable validation for development mode#17167
feat: Add environment variable validation for development mode#17167meet1785 wants to merge 1 commit intofacebook:mainfrom
Conversation
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.
|
Hi @meet1785! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
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.