Env Vars Cheatsheet - .env Config & dotenv Usage
The format and pitfalls you forget when writing .env or loading config with dotenv, grouped by definition, framework integration and security, with copy-ready examples.
Back to Config Formats变量定义 Definition 6
KEY=value定义变量(等号两侧不要有空格)
KEY="value with space"含空格或特殊字符需加引号
KEY='literal $NOT_EXPANDED'单引号内不做变量展开
KEY=${OTHER}_suffix双引号内可引用其他变量
# comment井号开头为注释行
export KEY=value设置并导出为环境变量
引用与读取 Reading 5
echo $KEYShell 中读取变量(Linux/macOS)
echo %KEY%Windows cmd 读取变量
$env:KEYPowerShell 读取变量
process.env.KEYNode.js 读取
os.getenv("KEY")Python 读取
dotenv 用法 4
require('dotenv').config()Node 加载 .env 到 process.env
import 'dotenv/config'ESM 写法加载 .env
dotenv.config({ path: '.env.local' })指定自定义文件路径
python-dotenv load_dotenv()Python 加载 .env
多环境文件 Multi-env 5
.env默认环境(勿提交到 git)
.env.local本地覆盖(优先级更高)
.env.development开发环境
.env.production生产环境
.env.example示例模板,可提交,不含真实密钥
安全陷阱 Security FAQ 4
不要提交密钥把 .env 加入 .gitignore,用 .env.example 提供模板
区分大小写KEY 与 key 是不同变量
类型转换所有值都是字符串,数字需自行 parseInt/Number()
换行需转义多行值用引号包裹或使用
💡 Tips
- No spaces around the equals sign (KEY=value, not KEY = value).
- Add .env to .gitignore; commit secrets only via .env.example.
- All values are strings — convert numbers with Number()/parseInt().
- For multi-line values, wrap in quotes or use \n for a newline.
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Aug 2, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it