重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
env-vars by vercel-labs/vercel-plugin
npx skills add https://github.com/vercel-labs/vercel-plugin --skill env-vars您是 Vercel 环境变量管理方面的专家 —— 精通 .env 文件规范、vercel env CLI、OIDC 令牌生命周期以及特定环境配置。
Vercel 和 Next.js 按特定顺序加载环境变量。后加载的文件会覆盖先加载的:
| 文件 | 用途 | 是否受 Git 跟踪? |
|---|---|---|
.env | 所有环境的默认值 | 是 |
.env.local | 本地覆盖项和密钥 | 否(被 gitignore 忽略) |
.env.development |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 开发环境特定的默认值 |
| 是 |
.env.development.local | 本地开发覆盖项 | 否 |
.env.production | 生产环境特定的默认值 | 是 |
.env.production.local | 本地生产环境覆盖项 | 否 |
.env.test | 测试环境特定的默认值 | 是 |
.env.test.local | 本地测试覆盖项 | 否 |
.env (优先级最低).env.[environment] (development、production 或 test).env.local (在 test 环境中跳过).env.[environment].local (优先级最高,在 test 环境中跳过).env、.env.development 或 .env.production —— 请使用 .local 变体或 Vercel 环境变量.env.local 始终被 Next.js 的 gitignore 忽略 —— 这是 vercel env pull 写入密钥的地方NEXT_PUBLIC_ 为前缀的变量会暴露给浏览器捆绑包 —— 切勿将密钥放入 NEXT_PUBLIC_ 变量中# 将当前环境的所有环境变量拉取到 .env.local
vercel env pull .env.local
# 为特定环境拉取
vercel env pull .env.local --environment=production
vercel env pull .env.local --environment=preview
vercel env pull .env.local --environment=development
# 在不提示的情况下覆盖现有文件
vercel env pull .env.local --yes
# 拉取到自定义文件
vercel env pull .env.production.local --environment=production
# 交互式 —— 提示输入值和环境
vercel env add MY_SECRET
# 非交互式
echo "secret-value" | vercel env add MY_SECRET production
# 添加到多个环境
echo "secret-value" | vercel env add MY_SECRET production preview development
# 添加敏感变量(加密,不在日志中显示)
vercel env add MY_SECRET --sensitive
# 列出所有环境变量
vercel env ls
# 按环境筛选
vercel env ls production
# 从特定环境移除
vercel env rm MY_SECRET production
# 从所有环境移除
vercel env rm MY_SECRET
从头开始设置项目时,请使用以下顺序:
# 1) 首先链接,以便拉取操作针对正确的 Vercel 项目
vercel link --yes --project <name-or-id> --scope <team>
# 2) 将环境变量拉取到 .env.local
vercel env pull .env.local --yes
# 3) 验证 .env.example 中所需的键是否存在于 .env.local 中
while IFS='=' read -r key _; do
[[ -z "$key" || "$key" == \#* ]] && continue
grep -q "^${key}=" .env.local || echo "Missing in .env.local: $key"
done < .env.example
如果您需要立即使用 Vercel 环境变量但还不想写入 .env.local:
vercel env run -- npm run dev
这在引导期间进行快速验证时很有用,但对于正常的本地工作流程,仍需拉取 .env.local。
创建/更新密钥(vercel env add、仪表板更改)或配置添加环境变量的集成(例如 Neon/Upstash)后,重新运行:
vercel env pull .env.local --yes
Vercel 使用 OIDC (OpenID Connect) 令牌在您的应用与 Vercel 服务(AI Gateway、存储等)之间进行安全、无密钥的身份验证。
VERCEL_OIDC_TOKEN 会自动注入为短期有效的 JWT 并自动刷新 —— 无需任何配置vercel env pull .env.local 会提供一个有效期约 12 小时的 VERCEL_OIDC_TOKENvercel env pull .env.local --yes 以获取新的令牌。建议在每个开发会话开始时重新拉取,以避免会话中途的身份验证失败// @vercel/oidc 包会自动读取 VERCEL_OIDC_TOKEN
import { getVercelOidcToken } from '@vercel/oidc'
// AI Gateway 默认使用 OIDC —— 无需手动处理令牌
import { gateway } from 'ai'
const result = await generateText({
model: gateway('openai/gpt-5.2'),
prompt: 'Hello',
})
| 症状 | 原因 | 修复方法 |
|---|---|---|
本地缺少 VERCEL_OIDC_TOKEN | 尚未拉取环境变量 | vercel env pull .env.local |
| 本地约 12 小时后出现身份验证错误 | 令牌已过期 | vercel env pull .env.local --yes |
| 在 Vercel 上工作,本地失败 | 令牌不在 .env.local 中 | vercel env pull .env.local |
AI_GATEWAY_API_KEY 与 OIDC | 两者都设置了,密钥优先 | 移除 AI_GATEWAY_API_KEY 以使用 OIDC |
| 使用场景 | 设置位置 |
|---|---|
| 密钥(API 密钥、令牌) | Vercel 仪表板 (https://vercel.com/{team}/{project}/settings/environment-variables) 或 vercel env add |
| 公共配置(站点 URL、功能标志) | .env 或 .env.[environment] 文件 |
| 仅限本地的覆盖项 | .env.local |
| CI/CD 密钥 | Vercel 仪表板 (https://vercel.com/{team}/{project}/settings/environment-variables),并指定环境范围 |
在 Vercel 仪表板 https://vercel.com/{team}/{project}/settings/environment-variables 设置的变量可以限定范围到:
vercel.app 生产部署vercel dev 和 vercel env pull一个变量可以分配给一个、两个或所有三个环境。
预览环境变量可以限定到特定的 Git 分支:
# 仅为 "staging" 分支添加变量
echo "staging-value" | vercel env add DATABASE_URL preview --git-branch=staging
vercel env pull 会覆盖自定义变量vercel env pull .env.local 会替换整个文件 —— 任何手动添加的变量(自定义密钥、本地覆盖项、调试标志)都会丢失。拉取后始终备份或重新添加自定义变量:
# 拉取前保存自定义变量
grep -v '^#' .env.local | grep -v '^VERCEL_\|^POSTGRES_\|^NEXT_PUBLIC_' > .env.custom.bak
vercel env pull .env.local --yes
cat .env.custom.bak >> .env.local # 重新附加自定义变量
或者将自定义变量维护在单独的 .env.development.local 文件中(由 Next.js 在 .env.local 之后加载)。
.env.local只有 Next.js 会自动加载 .env.local。独立脚本(drizzle-kit、tsx、自定义 Node 脚本)需要显式加载:
# 使用 dotenv-cli
npm install -D dotenv-cli
npx dotenv -e .env.local -- npx drizzle-kit push
npx dotenv -e .env.local -- npx tsx scripts/seed.ts
# 或者手动加载
source <(grep -v '^#' .env.local | sed 's/^/export /') && node scripts/migrate.js
vercel env pull 作为设置工作流的一部分 —— 在 README 中记录它.env.example —— 提交一个带有空值的模板,以便团队成员知道需要哪些变量NEXT_PUBLIC_ 前缀 —— 并且切勿将密钥放入其中.env.development.local 中 —— 保护它们不被 vercel env pull 覆盖每周安装量
50
仓库
GitHub 星标数
94
首次出现
2026年3月7日
安全审计
安装于
codex47
opencode47
gemini-cli46
amp46
cline46
github-copilot46
You are an expert in Vercel environment variable management — .env file conventions, the vercel env CLI, OIDC token lifecycle, and environment-specific configuration.
Vercel and Next.js load environment variables in a specific order. Later files override earlier ones:
| File | Purpose | Git-tracked? |
|---|---|---|
.env | Default values for all environments | Yes |
.env.local | Local overrides and secrets | No (gitignored) |
.env.development | Development-specific defaults | Yes |
.env.development.local | Local dev overrides | No |
.env.production | Production-specific defaults | Yes |
.env.production.local | Local prod overrides | No |
.env.test | Test-specific defaults | Yes |
.env.test.local | Local test overrides | No |
.env (lowest priority).env.[environment] (development, production, or test).env.local (skipped in test environment).env.[environment].local (highest priority, skipped in test).env, .env.development, or .env.production — use .local variants or Vercel environment variables.env.local is always gitignored by Next.js — this is where vercel env pull writes secretsNEXT_PUBLIC_ are exposed to the browser bundle — never put secrets in NEXT_PUBLIC_ vars# Pull all env vars for the current environment into .env.local
vercel env pull .env.local
# Pull for a specific environment
vercel env pull .env.local --environment=production
vercel env pull .env.local --environment=preview
vercel env pull .env.local --environment=development
# Overwrite existing file without prompting
vercel env pull .env.local --yes
# Pull to a custom file
vercel env pull .env.production.local --environment=production
# Interactive — prompts for value and environments
vercel env add MY_SECRET
# Non-interactive
echo "secret-value" | vercel env add MY_SECRET production
# Add to multiple environments
echo "secret-value" | vercel env add MY_SECRET production preview development
# Add a sensitive variable (encrypted, not shown in logs)
vercel env add MY_SECRET --sensitive
# List all environment variables
vercel env ls
# Filter by environment
vercel env ls production
# Remove from specific environment
vercel env rm MY_SECRET production
# Remove from all environments
vercel env rm MY_SECRET
Use this sequence when setting up a project from scratch:
# 1) Link first so pulls target the correct Vercel project
vercel link --yes --project <name-or-id> --scope <team>
# 2) Pull env vars into .env.local
vercel env pull .env.local --yes
# 3) Verify required keys from .env.example exist in .env.local
while IFS='=' read -r key _; do
[[ -z "$key" || "$key" == \#* ]] && continue
grep -q "^${key}=" .env.local || echo "Missing in .env.local: $key"
done < .env.example
If you need Vercel environment variables immediately but do not want to write .env.local yet:
vercel env run -- npm run dev
This is useful for quick validation during bootstrap, but still pull .env.local for a normal local workflow.
After creating/updating secrets (vercel env add, dashboard changes) or provisioning integrations that add env vars (for example Neon/Upstash), re-run:
vercel env pull .env.local --yes
Vercel uses OIDC (OpenID Connect) tokens for secure, keyless authentication between your app and Vercel services (AI Gateway, storage, etc.).
VERCEL_OIDC_TOKEN is automatically injected as a short-lived JWT and auto-refreshed — zero configuration neededvercel env pull .env.local provisions a VERCEL_OIDC_TOKEN valid for ~12 hoursvercel env pull .env.local --yes to get a fresh one. Consider re-pulling at the start of each dev session to avoid mid-session auth failures// The @vercel/oidc package reads VERCEL_OIDC_TOKEN automatically
import { getVercelOidcToken } from '@vercel/oidc'
// AI Gateway uses OIDC by default — no manual token handling needed
import { gateway } from 'ai'
const result = await generateText({
model: gateway('openai/gpt-5.2'),
prompt: 'Hello',
})
| Symptom | Cause | Fix |
|---|---|---|
VERCEL_OIDC_TOKEN missing locally | Haven't pulled env vars | vercel env pull .env.local |
| Auth errors after ~12h locally | Token expired | vercel env pull .env.local --yes |
| Works on Vercel, fails locally | Token not in .env.local | vercel env pull .env.local |
AI_GATEWAY_API_KEY vs OIDC |
| Use Case | Where to Set |
|---|---|
| Secrets (API keys, tokens) | Vercel Dashboard (https://vercel.com/{team}/{project}/settings/environment-variables) or vercel env add |
| Public config (site URL, feature flags) | .env or .env.[environment] files |
| Local-only overrides | .env.local |
| CI/CD secrets | Vercel Dashboard (https://vercel.com/{team}/{project}/settings/environment-variables) with environment scoping |
Variables set in the Vercel Dashboard at https://vercel.com/{team}/{project}/settings/environment-variables can be scoped to:
vercel.app production deploymentsvercel dev and vercel env pullA variable can be assigned to one, two, or all three environments.
Preview environment variables can be scoped to specific Git branches:
# Add a variable only for the "staging" branch
echo "staging-value" | vercel env add DATABASE_URL preview --git-branch=staging
vercel env pull Overwrites Custom Variablesvercel env pull .env.local replaces the entire file — any manually added variables (custom secrets, local overrides, debug flags) are lost. Always back up or re-add custom vars after pulling:
# Save custom vars before pulling
grep -v '^#' .env.local | grep -v '^VERCEL_\|^POSTGRES_\|^NEXT_PUBLIC_' > .env.custom.bak
vercel env pull .env.local --yes
cat .env.custom.bak >> .env.local # Re-append custom vars
Or maintain custom vars in a separate .env.development.local file (loaded after .env.local by Next.js).
.env.localOnly Next.js auto-loads .env.local. Standalone scripts (drizzle-kit, tsx, custom Node scripts) need explicit loading:
# Use dotenv-cli
npm install -D dotenv-cli
npx dotenv -e .env.local -- npx drizzle-kit push
npx dotenv -e .env.local -- npx tsx scripts/seed.ts
# Or source manually
source <(grep -v '^#' .env.local | sed 's/^/export /') && node scripts/migrate.js
vercel env pull as part of your setup workflow — document it in your README.env.example — commit a template with empty values so teammates know which vars are neededNEXT_PUBLIC_ — and never put secrets in them.env.development.local — protects them from vercel env pull overwritesWeekly Installs
50
Repository
GitHub Stars
94
First Seen
Mar 7, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex47
opencode47
gemini-cli46
amp46
cline46
github-copilot46
Azure 配额管理指南:服务限制、容量验证与配额增加方法
138,600 周安装
| Both set, key takes priority |
Remove AI_GATEWAY_API_KEY to use OIDC |