Deploy to Vercel with Claude Code
Claude Code streamlines Vercel deployments by generating vercel.json configurations, managing environment variables, optimizing build settings, and troubleshooting failed builds. With CLAUDE.md context about your project, it handles the full deployment lifecycle from configuration to production rollout.
The Problem
Vercel deployments fail for subtle reasons – misconfigured build commands, missing environment variables, incorrect output directories, or framework detection issues. You want Claude Code to configure and troubleshoot deployments, but without project-specific context it generates generic configurations that miss your custom build requirements.
Quick Solution
- Set up your CLAUDE.md with Vercel deployment context:
# Vercel Deployment
- Framework: Next.js 14 (App Router)
- Build command: `pnpm build`
- Output directory: .next
- Node.js version: 20.x
- Team: my-team
- Generate the Vercel configuration:
claude "Create a vercel.json for this Next.js project.
Include: rewrites for API routes, headers for CORS,
cron job for /api/cleanup running daily at midnight,
and environment variable references for DATABASE_URL
and API_KEY."
- Deploy with the Vercel CLI:
# Preview deployment
vercel
# Production deployment
vercel --prod
- Troubleshoot a failed build:
claude "The Vercel build failed with this error:
[paste build log]. Read vercel.json and package.json
to identify the misconfiguration."
- Set up environment variables programmatically:
claude "Add these environment variables to Vercel for
all environments: DATABASE_URL, API_KEY, REDIS_URL.
Use the vercel env command. Do NOT hardcode values --
prompt me for each value."
How It Works
Claude Code interacts with Vercel through two channels: generating configuration files (vercel.json, next.config.js) and executing the Vercel CLI for deployments and environment management.
The vercel.json file controls build settings, routing, headers, cron jobs, and serverless function configuration. Claude generates this based on your project structure and CLAUDE.md context. For Next.js projects, Claude detects the app router vs. pages router pattern and configures accordingly.
The deployment flow is: Claude reads your project structure, generates or updates vercel.json, verifies environment variables are set, runs the build locally to catch errors, and then triggers deployment via vercel --prod. The CLAUDE.md file provides the team name, framework version, and custom build requirements.
Preview deployments happen automatically on every vercel command (without --prod), creating a unique URL for testing. Claude can be configured to run preview deployments after every significant change by adding this to your hooks.
Common Issues
Build fails with “Module not found” errors. Vercel uses a clean install for each build. If your project depends on hoisted packages or workspace dependencies, they may be missing. Ensure your package.json lists all direct dependencies:
claude "The Vercel build fails with Module not found: sharp.
Check if sharp is listed in package.json dependencies
(not devDependencies). If not, add it and verify the
build works locally with a clean node_modules."
Environment variables missing in production. Vercel scopes environment variables to environments (development, preview, production). A variable set for preview will not be available in production. Use:
vercel env ls
vercel env add DATABASE_URL production
Build exceeds the 45-second function size or duration limits. Vercel serverless functions have size and timeout limits. Claude can help split large API routes into smaller functions or configure specific routes with increased limits in vercel.json:
{
"functions": {
"api/heavy-route.ts": {
"memory": 1024,
"maxDuration": 60
}
}
}
Example CLAUDE.md Section
# Vercel Deployment
## Project Config
- Framework: Next.js 14 (App Router)
- Build: `pnpm build`
- Node: 20.x
- Team: my-team (vercel --scope my-team)
## Environment Variables (all environments)
- DATABASE_URL: PostgreSQL connection string
- API_KEY: External API authentication
- NEXT_PUBLIC_APP_URL: Frontend URL (differs per env)
## Deployment Rules
- NEVER deploy to production without preview test first
- ALWAYS run `pnpm build` locally before deploying
- Check `vercel env ls` if build fails with undefined vars
- Use `vercel logs <url>` to debug runtime errors
## Known Issues
- Sharp requires explicit dependency (not hoisted)
- ISR revalidation needs NEXT_PUBLIC_APP_URL set correctly
- Cron jobs only work in production (not preview)
Best Practices
- Always test builds locally first before triggering a Vercel deployment. Run
pnpm buildto catch compilation errors that are cheaper to fix locally. - Use preview deployments for every PR. Configure Vercel’s GitHub integration to auto-deploy previews, then use Claude Code to verify the preview URL before promoting to production.
- Scope environment variables correctly. Use
vercel env lsto verify which variables are available in each environment. Missing production variables cause the majority of deployment failures. - Document Vercel-specific quirks in CLAUDE.md. Framework detection, function size limits, and ISR configuration are common gotchas that Claude handles better with explicit context.
- Use
vercel.jsonfor configuration over CLI flags. Declarative configuration in vercel.json is version-controlled, reviewable, and reproducible across team members.
Related Reading
- Claude Code Docker Compose Development Workflow
- Claude Code AWS Lambda Deployment Guide
- Claude Code Docker Build Failed Fix
Built by theluckystrike. More at zovo.one