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 FormatsDefinition 6
KEY=value定义variable(号两侧不要有空格)
KEY="value with space"含空格or特殊字符需加引号
KEY='literal $NOT_EXPANDED'单引号内不做variable展开
KEY=${OTHER}_suffix双引号内可引用其他variable
# comment井号beginning为注释行
export KEY=valueSet&Export为env var
Reading 5
echo $KEYShell 中Readvariable(Linux/macOS)
echo %KEY%Windows cmd Readvariable
$env:KEYPowerShell Readvariable
process.env.KEYNode.js Read
os.getenv("KEY")Python Read
dotenv Usage 4
require('dotenv').config()Node 加载 .env 到 process.env
import 'dotenv/config'ESM 写法加载 .env
dotenv.config({ path: '.env.local' })指定自定义File路径
python-dotenv load_dotenv()Python 加载 .env
Multi-env Files 5
.env默认环境(勿提交到 git)
.env.local本地overwrite(优先级更高)
.env.development开发环境
.env.production生产环境
.env.exampleexampletemplate,可提交,不含真实密钥
Security FAQ 4
不要提交密钥把 .env 加入 .gitignore,用 .env.example 提供template
区分大小写KEY & key 是不同variable
类型转换所有值都是string,数字需自行 parseInt/Number()
换行需转义多行值用引号package裹or使用
💡 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