Utilizing Environment Variables for Configuration Management
Adopt best practices for using environment variables to keep configuration settings secure and adaptable.
0 likes
4 views
Rule Content
{ "title": "Utilizing Environment Variables for Configuration Management", "description": "Adopt best practices for using environment variables to keep configuration settings secure and adaptable.", "category": "JavaScript Cursor Rules", "rules": [ { "name": "Use Environment Variables for Sensitive Information", "description": "Store sensitive data such as API keys, database credentials, and service endpoints in environment variables to keep them out of the codebase.", "examples": [ { "before": "const apiKey = 'your_api_key_here';", "after": "const apiKey = process.env.API_KEY;" } ] }, { "name": "Centralize Configuration Management", "description": "Create a dedicated configuration module that loads and validates environment variables, providing a single source of truth for configuration settings.", "examples": [ { "before": "const dbUrl = process.env.DB_URL; const apiKey = process.env.API_KEY;", "after": "const config = require('./config'); const dbUrl = config.dbUrl; const apiKey = config.apiKey;" } ] }, { "name": "Validate Environment Variables", "description": "Implement validation to ensure all required environment variables are set and correctly formatted, preventing runtime errors.", "examples": [ { "before": "const dbUrl = process.env.DB_URL; if (!dbUrl) { throw new Error('DB_URL is required'); }", "after": "const config = require('./config'); // config module handles validation" } ] }, { "name": "Use Descriptive Naming Conventions", "description": "Adopt clear and descriptive names for environment variables to enhance readability and maintainability.", "examples": [ { "before": "const db = process.env.DB;", "after": "const databaseUrl = process.env.DATABASE_URL;" } ] }, { "name": "Exclude .env Files from Version Control", "description": "Add .env files to .gitignore to prevent sensitive information from being committed to the repository.", "examples": [ { "before": "DATABASE_URL=postgres://user:pass@localhost:5432/db", "after": "# .gitignore\n.env" } ] }, { "name": "Document Required Environment Variables", "description": "Maintain documentation listing all required environment variables, their purpose, and acceptable values to assist developers and maintain consistency.", "examples": [ { "before": "No documentation available.", "after": "| Variable Name | Description | Required | Default |\n|---------------|-------------------------------|----------|------------|\n| DATABASE_URL | The URL for the database | Yes | |\n| API_KEY | API key for third-party API | Yes | |" } ] } ] }