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.

Config Formats·24 commands·Last updated 2026-08-02
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 $KEY
Shell 中读取变量(Linux/macOS)
echo %KEY%
Windows cmd 读取变量
$env:KEY
PowerShell 读取变量
process.env.KEY
Node.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