- Learn
- Stack Essentials
- Vercel
- Deploying to Vercel
Deploy your Next.js application to Vercel with automatic builds, preview URLs, and production deployments.
Deploying to Vercel
Deploy your Next.js application to production with automatic builds on every push.
Deployment Methods
1. Import from Git (Recommended)
Connect your GitHub/GitLab/Bitbucket repository:
Dashboard → Add New → Project → Import Git Repository
Automatic deployments:
- Push to main → Production deployment
- Push to branch → Preview deployment
- Pull request → Preview URL in PR
2. Vercel CLI
Deploy from command line:
# Install CLI
npm install -g vercel
# Login
vercel login
# Deploy (development)
vercel
# Deploy to production
vercel --prod
3. AI Tool Export
From AI builders:
Lovable:
Export → Deploy to Vercel → Follow prompts
Bolt.new:
Deploy → Vercel → Authorize
Git-Based Deployment
Initial Setup
- Create repository (if not exists):
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/user/repo.git
git push -u origin main
- Import to Vercel:
- Go to vercel.com/new
- Select repository
- Configure project settings
- Click Deploy
Project Settings
Vercel auto-detects Next.js, but you can customize:
Framework Preset: Next.js
Build Command: npm run build
Output Directory: (auto-detected)
Install Command: npm install
Node.js Version: 20.x
Build Settings
For custom build requirements:
// package.json
{
"scripts": {
"build": "next build",
"vercel-build": "prisma generate && next build"
}
}
Production Deployments
Trigger Production Deploy
Production deploys when you push to the production branch:
# Default production branch: main
git checkout main
git merge feature-branch
git push origin main
# → Production deployment started
Manual Production Deploy
From dashboard:
Project → Deployments → Three dots → Promote to Production
Or via CLI:
vercel --prod
Instant Rollback
Rollback to previous deployment:
Deployments → Previous deployment → Three dots → Instant Rollback
Rollback is instant—no rebuild required.
Preview Deployments
Automatic Previews
Every non-production branch gets a preview URL:
Branch: feature/new-ui
URL: my-app-git-feature-new-ui-username.vercel.app
PR Comments
Vercel adds deployment status to pull requests:
✓ Preview deployment ready
Visit: https://my-app-abc123.vercel.app
Preview Environment
Previews can use different environment variables:
Production: NEXT_PUBLIC_API_URL=https://api.example.com
Preview: NEXT_PUBLIC_API_URL=https://staging-api.example.com
Deployment Configuration
vercel.json
Create vercel.json in project root:
{
"buildCommand": "npm run build",
"outputDirectory": ".next",
"installCommand": "npm install",
"framework": "nextjs",
"regions": ["iad1"],
"headers": [
{
"source": "/api/(.*)",
"headers": [
{ "key": "Cache-Control", "value": "no-store" }
]
}
],
"redirects": [
{
"source": "/old-page",
"destination": "/new-page",
"permanent": true
}
]
}
Build Environment Variables
Set during build only:
{
"build": {
"env": {
"DATABASE_URL": "@database-url"
}
}
}
Regions
Deploy to specific regions:
{
"regions": ["iad1", "sfo1", "cdg1"]
}
Available regions:
iad1- Washington D.C.sfo1- San Franciscocdg1- Parishnd1- Tokyosyd1- Sydney
Monorepo Deployments
Multiple Projects
For monorepos with multiple apps:
my-monorepo/
├── apps/
│ ├── web/ → Project: my-app-web
│ └── docs/ → Project: my-app-docs
├── packages/
└── package.json
Root Directory Setting
Configure in project settings:
Root Directory: apps/web
Include/Exclude Files
// vercel.json in apps/web/
{
"ignoreCommand": "git diff HEAD^ HEAD --quiet ."
}
Serverless Functions
API Routes
API routes become serverless functions automatically:
// app/api/users/route.ts
export async function GET() {
return Response.json({ users: [] })
}
// Deployed to: /api/users
// Type: Serverless Function
Function Config
Configure function settings:
// app/api/heavy/route.ts
export const maxDuration = 60 // 60 seconds max
export const dynamic = 'force-dynamic'
export async function POST(request: Request) {
// Long-running operation
}
Memory and Timeout
// vercel.json
{
"functions": {
"app/api/heavy/route.ts": {
"memory": 1024,
"maxDuration": 60
}
}
}
Build Optimization
Caching
Vercel caches:
node_modules(based on lockfile).next/cache(build cache)- Custom cache directories
Incremental Builds
Next.js ISR works automatically:
// Revalidate every hour
export const revalidate = 3600
export default async function Page() {
const data = await fetch('...')
return <div>{data}</div>
}
Build Output
Check build output in dashboard:
Build Logs
├── Installing dependencies
├── Building (3 pages, 5 API routes)
├── Generating static pages
└── Deployment complete (45s)
Monitoring Deployments
Deployment Status
Building → Ready / Error
Building: Compiling your application
Ready: Deployment successful
Error: Build or deployment failed
Build Logs
View real-time build logs:
Dashboard → Deployments → Click deployment → Build Logs
Function Logs
Monitor serverless function executions:
Dashboard → Logs → Select function → View invocations
Troubleshooting
Build Failures
Common issues:
Missing dependencies:
# Ensure dependencies are in package.json
npm install missing-package --save
TypeScript errors:
# Fix locally first
npm run build
Environment variables:
# Add missing vars in dashboard
Settings → Environment Variables
504 Gateway Timeout
Function timeout (default 10s on Hobby):
// Increase timeout (Pro plan)
export const maxDuration = 60
// Or optimize code
export async function GET() {
// Break into smaller operations
}
Module Not Found
// vercel.json - include extra files
{
"includeFiles": ["data/**"]
}
Summary
- Git deployment: Auto-deploys on push to main
- Preview URLs: Every branch gets a unique URL
- Instant rollback: Revert without rebuilding
- Serverless: API routes become functions
- vercel.json: Configure builds, headers, redirects
Next Steps
Learn to manage environment variables securely in Vercel.