环境变量
Use 环境变量到 配置 applications 跨越不同 stages 不使用 code 更改.
你将学到
- 什么环境变量是 and 为什么它们是 used
- 如何 设置, 读取, and persist 环境变量
- Using .环境 files for 局部 development
- 最佳实践 for managing 配置
概念讲解
什么是环境变量?
Environment variables are key-value pairs stored in the operating system's environment. Any process running on the system can read them. They are the standard way to configure applications without hardcoding values in the source code. For example, a database URL, an API port, or a log level can all be set as environment variables so the same code runs in development, staging, and production with different settings.
Setting and Reading Variables
In the shell, you set an environment variable with 导出 KEY="值". Once exported, the variable is available to the current shell and all processes started from it. To read a variable, use $KEY in the shell or 处理.环境.KEY in Node.js, os.environ in Python, or os.Getenv in Go. The printenv command lists all current environment variables.
.env Files
For local development, teams use .环境 files — plain text files containing KEY=值 lines. Tools like dotenv (Node.js) and python-dotenv load these files at startup. Never commit .环境 files to version control — they often contain secrets. Add .环境 to your .gitignore and provide a .环境.示例 template with placeholder values instead.
Configuration Best Practices
Keep configuration separate from code. Use environment variables for anything that changes between environments: database connections, API keys, feature flags, and service ports. Give variables sensible defaults so the app works out of the box in development. Validate required variables at startup so the app fails fast with a clear message instead of crashing later with a cryptic error.
Environment Hierarchy
A typical project uses different variables per environment: development uses a local database, staging uses a test database, and production uses a managed cloud database. The same code reads all of them through environment variables. Tools like Docker, Kubernetes, and cloud platforms all support environment variable injection natively. This pattern — known as the Twelve-Factor App methodology — keeps configuration strictly separate from code, making applications portable across environments without any code changes or rebuilds.
示例代码
# Set environment variables in the shell
export NODE_ENV="production"
export PORT=3000
export DATABASE_URL="postgres://user:pass@localhost:5432/myapp"
export LOG_LEVEL="info"
# Read them in any process
echo "Running on port $PORT in $NODE_ENV mode"
printenv | grep -E "PORT|NODE_ENV" # show specific variables
# A .env file for local development
cat > .env << 'EOF'
NODE_ENV=development
PORT=3000
DATABASE_URL=postgres://localhost:5432/myapp_dev
LOG_LEVEL=debug
EOF
环境变量是 设置使用导出, 读取使用 $VAR, and 加载从 .环境 files in development. heredoc (<< 'EOF') 编写多个行 到 file.
动手试一试
- 环境 File Generator
Generate .环境 files for 不同环境.
- YAML 到 JSON
Convert YAML 配置 files 到 JSON 格式.
- Base64 Encoder
Encode secrets for 环境变量 storage.
常见错误
错误写法
Committing .环境 files 使用 secrets 到 git
正确做法
添加 .环境到 .gitignore immediately. Provide .环境.示例使用 fake 值. 如果 secret 是 already committed, rotate 它 — assume 它是 compromised.
错误写法
Hardcoding 配置值 in 源 code
正确做法
读取所有环境-特定值 从环境变量. 这个 lets 相同 code 运行 in dev, staging, and production 不使用 code 更改.
练习 环境变量
2 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。