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
定义variable(号两侧不要有空格)
KEY="value with space"
含空格or特殊字符需加引号
KEY='literal $NOT_EXPANDED'
单引号内不做variable展开
KEY=${OTHER}_suffix
双引号内可引用其他variable
# comment
井号beginning为注释行
export KEY=value
Set&Export为env var

Reading 5

echo $KEY
Shell 中Readvariable(Linux/macOS)
echo %KEY%
Windows cmd Readvariable
$env:KEY
PowerShell Readvariable
process.env.KEY
Node.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.example
exampletemplate,可提交,不含真实密钥

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