Deploy Next.js to Vercel
To deploy Next.js to Vercel means moving your app from your local computer to a server that anyone on the internet can reach. Vercel is the platform built by the creators of Next.js, and it handles most of the setup for you. You connect a Git repository, and Vercel builds and hosts your app automatically.
This tutorial walks you through the complete workflow. You will run a production build locally to catch errors early, push your code to GitHub, and import the project into Vercel. You will also set environment variables, understand preview deployments, add a custom domain, and learn how to read build logs when something fails.
If you have not connected a database yet, read Next.js Database with Prisma first, because you will need to set the DATABASE_URL in Vercel. For making your deployed pages search-friendly, see Next.js Metadata & SEO.
What Does It Mean to Deploy a Next.js App?
Deployment is the process of taking your finished code and making it available on a live URL. In development, your app runs on localhost with hot reload and debugging tools. In production, it runs on a remote server with optimizations enabled, and anyone can visit it .
Before deploying, you should always test a production build locally. This catches errors that do not appear in development mode, such as strict TypeScript checks or missing environment variables.
# Terminal
npm run build
# Output: A route table showing each page and whether it is static or dynamic
npm run start
# Output: ▲ Next.js 16.x.x
# - Local: http://localhost:3000
Output: The build command compiles your app and prints a table of routes. The start command runs the production server. Open http://localhost:3000 to test the real production behavior.
How it works: next build runs TypeScript checks, ESLint, and compiles your code for production. It also prerenders static pages and generates the route table. If there are type errors or lint errors, the build fails, and you must fix them before deploying . next start serves the built output. It does not watch files for changes, so it behaves like the real deployed app.
Pushing Your Project to GitHub
Vercel deploys from a Git repository. If your project is not on GitHub yet, create a repository and push your code. Make sure your .gitignore file includes .env and .env.local so secrets are not committed.
# Terminal
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/your-username/your-repo.git
git push -u origin main
Output: Your code is now on GitHub. The main branch contains your latest commit, and Vercel can access it.
How it works: git init creates a local repository. git add . stages all files except those in .gitignore. git commit saves a snapshot. git remote add connects your local repo to GitHub, and git push uploads it. Once the code is on GitHub, Vercel can watch the repository for changes.
Importing the Repository in Vercel
Create a free Vercel account using your GitHub login. Then import the repository from the Vercel dashboard. The dashboard layout changes over time, but the flow is always the same: connect GitHub, choose a repository, and confirm the build settings .
# General Vercel import flow
1. Log in to Vercel with GitHub
2. Click "Add New" or "Import Project"
3. Select your GitHub repository
4. Vercel auto-detects Next.js and fills in build settings
5. Click "Deploy"
Output: Vercel builds your project and gives you a URL like your-project.vercel.app. The first deployment is always a production deployment.
How it works: Vercel detects that your project uses Next.js and configures the build command and output directory automatically. It runs next build on its servers and serves the result. You do not need to change any settings for a standard Next.js app .
Setting Environment Variables in Vercel
Environment variables are key-value pairs stored outside your code. Your database URL, API keys, and session secrets must be set in Vercel so the deployed app can read them. Never commit these values to Git .
# .env.local (on your machine, never committed)
DATABASE_URL="postgresql://user:password@host:5432/mydb"
SESSION_SECRET="a-long-random-string"
NEXT_PUBLIC_SITE_URL="https://your-domain.com"
Output: On Vercel, go to your project settings and find the Environment Variables section. Add each key and value. Choose which environments they apply to: Production, Preview, or Development .
How it works: Vercel injects these variables into the build and runtime environments. The NEXT_PUBLIC_ prefix means the variable is exposed to the browser. Variables without that prefix stay on the server. After adding or changing a variable, you must redeploy for the new value to take effect. Existing deployments do not update automatically .
Automatic Deployments and Preview URLs
Once your repository is connected, Vercel deploys automatically. Every push to main (or your production branch) creates a new production deployment. Every push to another branch or a pull request creates a preview deployment with its own URL .
# Terminal
# Create a feature branch and push
git checkout -b feature/new-header
git add .
git commit -m "Add new header"
git push -u origin feature/new-header
Output: Vercel creates a preview deployment for the feature/new-header branch. The URL appears in the Vercel dashboard and in the pull request comments on GitHub.
How it works: Preview deployments let you test changes in a live environment without affecting your production site. They use preview environment variables, which can be different from production values. This is useful for testing against a staging database or a sandbox API. When you merge the pull request into main, Vercel creates a new production deployment .
Adding a Custom Domain
A custom domain replaces the default .vercel.app URL with your own name, like yourdomain.com. You add it in the project settings under the Domains section, then update your DNS records .
# General custom domain flow
1. Go to Project Settings → Domains
2. Enter your domain name
3. Vercel shows the DNS records you need
4. Add those records at your domain registrar
5. Wait for DNS to propagate
6. Vercel automatically provisions an SSL certificate
Output: Your domain points to your Vercel deployment with HTTPS enabled. Visitors see your site at the custom domain.
How it works: Vercel needs you to prove you own the domain. For an apex domain like example.com, you add an A record. For a subdomain like www.example.com, you add a CNAME record. After DNS verification succeeds, Vercel issues an SSL certificate automatically. DNS changes can take some time to propagate .
Reading Build Logs and Fixing Common Errors
When a deployment fails, Vercel shows the build log. The most common failures are TypeScript errors and ESLint errors. These appear in the log with the file name and line number.
# Example build error in the Vercel log
Failed to compile.
./app/page.tsx:12:20
Type error: Property 'nmae' does not exist on type 'User'.
10 | return (
11 | <div>
> 12 | <p>{user.nmae}</p>
| ^
13 | </div>
14 | );
Output: The build fails and the deployment does not go live. The error message points to the exact file and line.
How it works: Next.js runs TypeScript and ESLint during next build. If either finds an error, the build stops. Fix the error locally, run npm run build again to confirm, then push the fix. Vercel will rebuild automatically. If you need to bypass a check temporarily, you can configure next.config.ts to ignore build errors, but this is not recommended because it hides real problems .
Comparing Hosting Options
| Option | How it works | Best for |
|---|---|---|
| Vercel | Connect a Git repo; Vercel builds and hosts automatically | Most Next.js projects; zero-config deployment |
| Self-hosted Node server | Run next build and next start on your own server |
Full control; existing infrastructure |
| Docker / standalone output | Build a container image with output: "standalone" |
Kubernetes, container platforms, portable deployments |
How it works: Vercel is the simplest path because it handles builds, SSL, CDN, and previews for you. A self-hosted Node server gives you full control but requires you to manage the server, HTTPS, and deployments. Docker with standalone output packages only the files needed to run, which makes the image smaller and portable across container platforms .
Common Mistakes and Best Practices
- Committing
.envor.env.local. These files contain secrets. If they end up on GitHub, anyone can read your database password. Add them to.gitignoreand set variables in Vercel instead . - Forgetting to redeploy after changing environment variables. Vercel does not update existing deployments when you add or change a variable. You must trigger a new deployment for the change to take effect .
- Not running
npm run buildlocally before pushing. TypeScript or ESLint errors that you did not see in development will fail the Vercel build. Run the production build locally first to catch them early . - Assuming preview deployments are public. By default, Vercel protects preview deployments with authentication. Only team members can view them unless you change the protection settings. Production deployments are public .
- Using a relative URL for the database or API. In production, relative URLs may not resolve correctly. Use absolute URLs or environment variables for external services.
- Ignoring the build log. The error message usually tells you exactly what is wrong and where. Read the first error, not just the last line, because later errors are often caused by the first one.
Real-World Example: Deploying a Blog App
This example shows the complete terminal workflow for deploying a blog app that uses Prisma and environment variables.
# .env.local (on your machine, not committed)
DATABASE_URL="postgresql://user:password@host:5432/blogdb"
SESSION_SECRET="your-secret-here"
# Terminal
# 1. Test the production build locally
npm run build
# 2. If the build passes, commit and push
git add .
git commit -m "Ready for deployment"
git push origin main
# 3. Vercel deploys automatically from GitHub
# 4. Set environment variables in Vercel dashboard:
# - DATABASE_URL (Secret)
# - SESSION_SECRET (Secret)
# 5. Redeploy after adding variables
# (Vercel dashboard: Deployments → Redeploy)
Line-by-line explanation:
- The
.env.localfile holds secrets for local development. It is gitignored and never pushed. npm run buildruns the production build. If it passes, your code is ready for Vercel.- The
git pushtriggers an automatic deployment because the repository is connected to Vercel. - In the Vercel dashboard, add
DATABASE_URLandSESSION_SECRETas Secret environment variables. They will not be visible in build logs. - After adding variables, redeploy so the new values are injected into the build and runtime.
Summary
- Deploy Next.js to Vercel by connecting a GitHub repository. Vercel builds and hosts your app automatically.
- Always run
npm run buildandnpm run startlocally to test the production build before pushing. - Set environment variables in the Vercel dashboard. Never commit
.envfiles to Git. - Every push to
maincreates a production deployment. Other branches and pull requests create preview deployments. - Add a custom domain in project settings and update your DNS records. Vercel handles SSL automatically.
- Read build logs carefully. TypeScript and ESLint errors are the most common causes of failed deployments.
Your app is now live. The final step is putting everything together in a complete project. Continue with Next.js Mini Project: Blog App.
Frequently Asked Questions
Do I need to configure anything special for Next.js on Vercel?
No. Vercel automatically detects Next.js and configures the build command and output directory. For a standard App Router project, you can use the default settings. The only thing you must do is add your environment variables in the dashboard. If your project uses a custom build command or monorepo structure, you may need to adjust the root directory in the project settings.
Why did my deployment fail with a TypeScript error?
Vercel runs TypeScript checks during the build. If your code has type errors that were ignored in development, the production build fails. Run npm run build locally to see the same error with the exact file and line number. Fix the error, confirm the local build passes, then push the fix. Vercel will rebuild automatically.
How do I update environment variables on Vercel?
Go to your project settings and find the Environment Variables section. Edit the value or add a new variable. After saving, you must redeploy for the change to take effect. Existing deployments keep the old values. You can trigger a redeploy from the Deployments tab in the dashboard.
What is the difference between a production and preview deployment?
A production deployment is the live version of your site that visitors see at your main domain. A preview deployment is a separate build for a branch or pull request, with its own URL. Preview deployments are protected by default and are useful for testing changes before merging. When you push to your production branch, Vercel creates a new production deployment.
Can I deploy Next.js somewhere other than Vercel?
Yes. Next.js can run on any platform that provides a Node.js server. You can self-host by running next build and next start on a server. You can also build a Docker image using output: "standalone" for container platforms. Vercel is the easiest option, but it is not the only one.
Practice Exercises
Easy: Run a production build locally
Run npm run build in your project. If it passes, run npm run start and open http://localhost:3000. What do you see?
Show solution
# Terminal
npm run build
npm run start
# Output: ▲ Next.js 16.x.x
# - Local: http://localhost:3000
The production server runs your built app. You should see the same pages as in development, but with production optimizations and no hot reload.
Easy: Create a .gitignore entry for .env
Check your .gitignore file. Make sure it contains .env and .env.local. If not, add them.
Show solution
# .gitignore (add these lines if missing)
.env
.env.local
.env*.local
These lines prevent your secret environment variables from being committed to Git and pushed to GitHub.
Medium: Add an environment variable in Vercel
Add a variable called NEXT_PUBLIC_SITE_NAME with the value "My Blog". Set it for the Production environment. Then redeploy and verify it appears on your site.
Show solution
# Steps in Vercel dashboard
1. Project Settings → Environment Variables
2. Key: NEXT_PUBLIC_SITE_NAME
3. Value: My Blog
4. Environment: Production (and Preview if needed)
5. Save
6. Go to Deployments → Redeploy
// app/page.tsx (example usage)
export default function HomePage() {
const siteName = process.env.NEXT_PUBLIC_SITE_NAME ?? "Default";
return <h1>{siteName}</h1>;
}
The NEXT_PUBLIC_ prefix makes the variable available in the browser. Redeploy after adding it so the new value is included in the build.
Challenge: Fix a build error and redeploy
Intentionally introduce a TypeScript error in a page, such as a misspelled property. Run npm run build locally, read the error, fix it, and confirm the build passes. Then push the fix and verify the Vercel deployment succeeds.
Show solution
// app/page.tsx (before fix: intentional error)
type User = { name: string };
export default function HomePage() {
const user: User = { name: "Asha" };
return <p>{user.nmae}</p>; // Error: Property 'nmae' does not exist
}
// app/page.tsx (after fix)
type User = { name: string };
export default function HomePage() {
const user: User = { name: "Asha" };
return <p>{user.name}</p>;
}
The local build catches the error before it reaches Vercel. Fixing it and pushing the correction triggers a successful deployment.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts.
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
