vercel-cli-with-tokens
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVercel CLI with Tokens
基于令牌的Vercel CLI使用方法
Deploy and manage projects on Vercel using the CLI with token-based authentication. Use this skill when you have a Vercel access token and need to operate without interactive login.
使用CLI通过基于令牌的身份验证在Vercel上部署和管理项目。当你拥有Vercel访问令牌且需要无需交互式登录进行操作时,可使用此方法。
Authentication
身份验证
Never run . Always use token-based auth via the flag or environment variable. This ensures the correct account is used even if another Vercel account is logged in locally.
vercel login--tokenVERCEL_TOKENbash
vercel <command> --token "$VERCEL_TOKEN"切勿运行命令。 请始终通过参数或环境变量使用基于令牌的身份验证。这样即使本地已登录其他Vercel账户,也能确保使用正确的账户进行操作。
vercel login--tokenVERCEL_TOKENbash
vercel <command> --token "$VERCEL_TOKEN"Scoping to a Team
指定团队
Use to target a specific team. Accepts a team slug or team ID ():
--scopeteam_...bash
vercel <command> --token "$VERCEL_TOKEN" --scope <team-slug-or-id>Not required if the project is already linked ( provides the org context) or if is set.
.vercel/project.jsonVERCEL_ORG_ID使用参数定位到特定团队。支持传入团队别名或团队ID(格式为):
--scopeteam_...bash
vercel <command> --token "$VERCEL_TOKEN" --scope <team-slug-or-id>如果项目已关联(文件中包含组织上下文)或已设置环境变量,则无需此参数。
.vercel/project.jsonVERCEL_ORG_IDTargeting a Project
指定项目
Use to target a specific project without needing . Accepts a project name or project ID ():
--projectvercel linkprj_...bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> --project <project-name-or-id>This is the simplest path when you already have a project ID — no directory needed.
.vercel/使用参数直接定位到特定项目,无需执行命令。支持传入项目名称或项目ID(格式为):
--projectvercel linkprj_...bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> --project <project-name-or-id>当你已拥有项目ID时,这是最简单的操作方式——无需创建目录。
.vercel/Environment Variable Alternative
环境变量替代方案
Instead of flags, you can set these environment variables. The CLI recognizes them natively:
| Variable | Purpose | Equivalent flag |
|---|---|---|
| Auth token | |
| Team/org ID | |
| Project ID | |
VERCEL_ORG_IDVERCEL_PROJECT_ID.vercel/project.json除了使用命令行参数,你也可以设置以下环境变量,CLI会自动识别它们:
| 变量名 | 用途 | 对应命令行参数 |
|---|---|---|
| 身份验证令牌 | |
| 团队/组织ID | |
| 项目ID | |
VERCEL_ORG_IDVERCEL_PROJECT_ID.vercel/project.jsonResolution Precedence
优先级顺序
Token: flag > env var > stored auth in config.
--tokenVERCEL_TOKENTeam/scope: flag > in > in global config > .
--scopescopevercel.jsoncurrentTeamVERCEL_ORG_IDProject: flag > env var > > interactive prompt.
--projectVERCEL_PROJECT_ID.vercel/project.json令牌优先级: 参数 > 环境变量 > 配置文件中存储的身份验证信息。
--tokenVERCEL_TOKEN团队/范围优先级: 参数 > 中的配置 > 全局配置中的 > 环境变量。
--scopevercel.jsonscopecurrentTeamVERCEL_ORG_ID项目优先级: 参数 > 环境变量 > 文件 > 交互式提示。
--projectVERCEL_PROJECT_ID.vercel/project.jsonCLI Setup
CLI安装配置
bash
npm install -g vercel
vercel --versionOr use for one-off commands:
npxbash
npx vercel <command> --token "$VERCEL_TOKEN"bash
npm install -g vercel
vercel --version或者使用执行一次性命令:
npxbash
npx vercel <command> --token "$VERCEL_TOKEN"Deploying a Project
项目部署
Always deploy as preview unless the user explicitly requests production.
除非用户明确要求生产环境部署,否则默认部署为预览环境。
Quick Deploy (have project ID — no linking needed)
快速部署(已有项目ID——无需关联)
When you already have a project ID (e.g., ), deploy directly:
prj_...bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> --project <project-id> -y --no-waitCheck status:
bash
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"Production deploy (only when explicitly requested):
bash
vercel deploy --prod --token "$VERCEL_TOKEN" --scope <team> --project <project-id> -y --no-wait当你已拥有项目ID(例如)时,可直接部署:
prj_...bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> --project <project-id> -y --no-wait查看部署状态:
bash
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"生产环境部署(仅在用户明确要求时执行):
bash
vercel deploy --prod --token "$VERCEL_TOKEN" --scope <team> --project <project-id> -y --no-waitFull Deploy Flow (no project ID)
完整部署流程(无项目ID)
Use this when you have a token and team but need to create or find a project.
当你拥有令牌和团队信息,但需要创建或查找项目时,使用此流程。
Step 1: Determine Project State
步骤1:确认项目状态
bash
undefinedbash
undefined1. Does the project have a git remote?
1. 项目是否关联了Git远程仓库?
git remote get-url origin 2>/dev/null
git remote get-url origin 2>/dev/null
2. Is it already linked to a Vercel project?
2. 项目是否已关联到Vercel项目?
cat .vercel/project.json 2>/dev/null || cat .vercel/repo.json 2>/dev/null
undefinedcat .vercel/project.json 2>/dev/null || cat .vercel/repo.json 2>/dev/null
undefinedStep 2: Link the Project
步骤2:关联项目
If no or exists, link first.
.vercel/project.json.vercel/repo.jsonWith git remote (preferred):
bash
vercel link --repo --token "$VERCEL_TOKEN" --scope <team> -yReads the git remote and connects to the matching Vercel project. Creates . More reliable than without , which matches by directory name.
.vercel/repo.jsonvercel link--repoWithout git remote:
bash
vercel link --token "$VERCEL_TOKEN" --scope <team> -yCreates .
.vercel/project.jsonLink to a specific existing project by name:
bash
vercel link --project <project-name> --token "$VERCEL_TOKEN" --scope <team> -yIf the project is already linked, check in or to verify it matches the intended team. If not, re-link with the correct .
orgId.vercel/project.json.vercel/repo.json--scope如果不存在或文件,需先执行关联操作。
.vercel/project.json.vercel/repo.json通过Git远程仓库关联(推荐):
bash
vercel link --repo --token "$VERCEL_TOKEN" --scope <team> -y该命令会读取Git远程仓库信息,并关联到对应的Vercel项目,同时创建文件。相比不带参数的命令,此方式更可靠,后者仅通过目录名匹配项目。
.vercel/repo.json--repovercel link无Git远程仓库时关联:
bash
vercel link --token "$VERCEL_TOKEN" --scope <team> -y该命令会创建文件。
.vercel/project.json关联到指定现有项目(通过项目名称):
bash
vercel link --project <project-name> --token "$VERCEL_TOKEN" --scope <team> -y如果项目已关联,需检查或中的是否与目标团队一致。若不一致,需使用正确的参数重新关联。
.vercel/project.json.vercel/repo.jsonorgId--scopeStep 3: Deploy
步骤3:部署项目
A) Git Push Deploy — has git remote (preferred)
The best long-term setup. Git pushes trigger automatic Vercel deployments.
- Ask the user before pushing. Never push without explicit approval.
- Commit and push:
bash
git add . git commit -m "deploy: <description of changes>" git push - Vercel builds automatically. Non-production branches get preview deployments; the production branch (usually ) gets a production deployment.
main - Retrieve the deployment URL:
The latest entry in thebash
sleep 5 vercel ls --format json --token "$VERCEL_TOKEN" --scope <team>array has the preview URL.deployments
B) CLI Deploy — no git remote
bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> -y --no-wait--no-waitbash
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"A) Git推送部署(已有Git远程仓库——推荐)
这是长期维护项目的最佳方式,Git推送会自动触发Vercel部署。
- 推送前需征得用户同意,切勿未经许可直接推送。
- 提交并推送代码:
bash
git add . git commit -m "deploy: <变更描述>" git push - Vercel会自动构建项目。非生产分支会生成预览部署链接;生产分支(通常为)会生成生产部署链接。
main - 获取部署链接:
bash
sleep 5 vercel ls --format json --token "$VERCEL_TOKEN" --scope <team>数组中的最新条目即为预览部署链接。deployments
B) CLI部署(无Git远程仓库)
bash
vercel deploy --token "$VERCEL_TOKEN" --scope <team> -y --no-wait--no-waitbash
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"Deploying from a Remote Repository
从远程仓库部署
If the user wants to deploy code from a remote that isn't cloned locally:
- Clone the repository:
bash
git clone <repo-url> cd <repo-name> - Link to Vercel:
bash
vercel link --repo --token "$VERCEL_TOKEN" --scope <team> -y - Deploy via git push (if you have push access) or CLI deploy.
如果用户希望从本地未克隆的远程仓库部署代码:
- 克隆仓库:
bash
git clone <repo-url> cd <repo-name> - 关联到Vercel:
bash
vercel link --repo --token "$VERCEL_TOKEN" --scope <team> -y - 通过Git推送(若有推送权限)或CLI命令完成部署。
About .vercel/
Directory
.vercel/关于.vercel/
目录
.vercel/A linked project has either:
- — from
.vercel/project.json. Containsvercel linkandprojectId.orgId - — from
.vercel/repo.json. Containsvercel link --repo,orgId, and aremoteNamemap.projects
Either file means the project is linked. Not needed when using flag or + env vars.
--projectVERCEL_ORG_IDVERCEL_PROJECT_IDDo NOT run , , or in an unlinked directory to detect state — without , they will interactively prompt or silently link as a side-effect. Only is safe to run anywhere.
vercel project inspectvercel lsvercel link.vercel/vercel whoami --token "$VERCEL_TOKEN"已关联的项目会包含以下两种文件之一:
- —— 由
.vercel/project.json命令生成,包含vercel link和projectId。orgId - —— 由
.vercel/repo.json命令生成,包含vercel link --repo、orgId和remoteName映射表。projects
只要存在其中一个文件,就表示项目已关联。当使用参数或同时设置和环境变量时,无需此目录。
--projectVERCEL_ORG_IDVERCEL_PROJECT_ID请勿在未关联的目录中执行、或命令来检测项目状态——如果没有目录,这些命令会触发交互式提示或自动静默关联项目。只有命令可以在任意目录安全执行。
vercel project inspectvercel lsvercel link.vercel/vercel whoami --token "$VERCEL_TOKEN"Managing Environment Variables
环境变量管理
Run from a linked project directory, or use to target a specific project.
--projectbash
undefined可在已关联的项目目录中执行以下命令,或使用参数定位到特定项目。
--projectbash
undefinedSet for all environments
为所有环境设置环境变量
echo "value" | vercel env add VAR_NAME --token "$VERCEL_TOKEN" --scope <team>
echo "value" | vercel env add VAR_NAME --token "$VERCEL_TOKEN" --scope <team>
Set for a specific environment (production, preview, development)
为特定环境设置环境变量(production、preview、development)
echo "value" | vercel env add VAR_NAME production --token "$VERCEL_TOKEN" --scope <team>
echo "value" | vercel env add VAR_NAME production --token "$VERCEL_TOKEN" --scope <team>
List environment variables
列出所有环境变量
vercel env ls --token "$VERCEL_TOKEN" --scope <team>
vercel env ls --token "$VERCEL_TOKEN" --scope <team>
Pull env vars to local .env file
将环境变量拉取到本地.env文件
vercel env pull --token "$VERCEL_TOKEN" --scope <team>
vercel env pull --token "$VERCEL_TOKEN" --scope <team>
Remove a variable
删除环境变量
vercel env rm VAR_NAME --token "$VERCEL_TOKEN" --scope <team> -y
undefinedvercel env rm VAR_NAME --token "$VERCEL_TOKEN" --scope <team> -y
undefinedInspecting Deployments
部署状态查看
bash
undefinedbash
undefinedList recent deployments
列出最近的部署记录
vercel ls --format json --token "$VERCEL_TOKEN" --scope <team>
vercel ls --format json --token "$VERCEL_TOKEN" --scope <team>
Inspect a specific deployment
查看特定部署的详细信息
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"
vercel inspect <deployment-url> --token "$VERCEL_TOKEN"
View build logs
查看构建日志
vercel logs <deployment-url> --token "$VERCEL_TOKEN"
undefinedvercel logs <deployment-url> --token "$VERCEL_TOKEN"
undefinedManaging Domains
域名管理
bash
undefinedbash
undefinedList domains
列出所有域名
vercel domains ls --token "$VERCEL_TOKEN" --scope <team>
vercel domains ls --token "$VERCEL_TOKEN" --scope <team>
Add a domain to the project
为项目添加域名
vercel domains add <domain> --token "$VERCEL_TOKEN" --scope <team>
undefinedvercel domains add <domain> --token "$VERCEL_TOKEN" --scope <team>
undefinedWorking Agreement
操作规范
- Always use on every Vercel CLI command. Never rely on local login state.
--token - Use and/or
--scopeto target the correct team and project.--project - Do not run . Authentication is handled entirely by the access token.
vercel login - Default to preview deployments. Only deploy to production when explicitly asked.
- Ask before pushing to git. Never push commits without the user's approval.
- Do not read or modify files directly. The CLI manages this directory.
.vercel/ - Do not curl/fetch deployed URLs to verify. Just return the link to the user.
- Use when structured output will help with follow-up steps (e.g.,
--format json).vercel ls - Use on commands that prompt for confirmation to avoid interactive blocking.
-y
- 始终在Vercel CLI命令中使用参数,切勿依赖本地登录状态。
--token - 使用和/或
--scope参数定位到正确的团队和项目。--project - 切勿运行命令,所有身份验证操作均通过访问令牌完成。
vercel login - 默认部署到预览环境,仅在用户明确要求时部署到生产环境。
- 推送Git代码前需征得用户同意,切勿未经许可直接推送提交。
- 请勿直接读取或修改目录下的文件,该目录由CLI自动管理。
.vercel/ - 请勿通过curl/fetch访问部署链接来验证,直接将链接返回给用户即可。
- 当结构化输出有助于后续操作时,使用参数(例如
--format json命令)。vercel ls - 在需要确认的命令中使用参数,避免交互式阻塞。
-y
Troubleshooting
故障排查
Authentication Error
身份验证错误
If commands fail with or similar:
Authentication required- The token may be expired or revoked. Ask the user for a fresh token or check the token source.
- Verify the token is valid:
vercel whoami --token "$VERCEL_TOKEN"
如果命令执行失败并提示或类似信息:
Authentication required- 令牌可能已过期或被撤销,请向用户索要新的令牌或检查令牌来源。
- 验证令牌有效性:
vercel whoami --token "$VERCEL_TOKEN"
Wrong Team
团队错误
If deployments appear under the wrong team, verify is correct:
--scopebash
vercel whoami --token "$VERCEL_TOKEN" --scope <team>如果部署记录显示在错误的团队下,请验证参数是否正确:
--scopebash
vercel whoami --token "$VERCEL_TOKEN" --scope <team>Build Failure
构建失败
Check the build logs:
bash
vercel logs <deployment-url> --token "$VERCEL_TOKEN"Common causes:
- Missing dependencies — ensure is complete and committed.
package.json - Missing environment variables — add them with .
vercel env add - Framework misconfiguration — check or framework-specific settings.
vercel.json - Vercel auto-detects frameworks (Next.js, Remix, Vite, etc.) from . Override with a
package.jsonif detection is wrong.vercel.json
查看构建日志:
bash
vercel logs <deployment-url> --token "$VERCEL_TOKEN"常见原因:
- 依赖缺失——确保文件完整且已提交。
package.json - 环境变量缺失——使用命令添加。
vercel env add - 框架配置错误——检查或框架特定配置。
vercel.json - Vercel会从自动识别框架(Next.js、Remix、Vite等)。如果识别错误,可通过
package.json文件手动指定。vercel.json
CLI Not Installed
CLI未安装
bash
npm install -g vercelbash
npm install -g vercel