Skip to content

Next.js 环境变量

.env 文件约定

.env                # 所有环境默认
.env.local          # 本地覆盖(gitignore)
.env.development    # next dev
.env.production     # next start

前缀规则

前缀客户端可见
无前缀仅服务端
NEXT_PUBLIC_客户端可见
bash
DATABASE_URL=xxx          # 仅服务端
NEXT_PUBLIC_API_URL=xxx   # 客户端 + 服务端

客户端读取

tsx
// 只能用 NEXT_PUBLIC_ 前缀
console.log(process.env.NEXT_PUBLIC_API_URL)