Netlify
Learn how to deploy your application to Netlify.
Netlify is a popular platform for deploying web applications. While it's optimized for static sites, you can deploy Next.js applications using Netlify's Next.js runtime.
Why Netlify?
- Easy deployment - Connect GitHub and deploy automatically
- Free tier available - Great for getting started
- Automatic HTTPS - SSL certificates included
- Edge functions - Run serverless functions at the edge
- Preview deployments - Automatic previews for PRs
Deploying to Netlify
1. Create Netlify Account
Sign up for a free account at netlify.com.
2. Create New Site
- Click "Add new site" → "Import an existing project"
- Connect your Git provider (GitHub, GitLab, or Bitbucket)
- Select your repository
3. Configure Build Settings
Netlify will auto-detect Next.js, but verify these settings:
- Build command:
npm run build - Publish directory:
.next - Framework preset: Next.js
For Next.js standalone output, you may need to adjust:
- Build command:
npm run build && npm run export(if using static export) - Or use Netlify's Next.js runtime (recommended)
4. Add Environment Variables
Add all required environment variables:
- Go to Site settings → Environment variables
- Add variables from your
.env:
DATABASE_URL=postgresql://...
BETTER_AUTH_SECRET=...
NEXT_PUBLIC_SITE_URL=https://your-app.netlify.app
STRIPE_SECRET_KEY=...
STRIPE_PUBLISHABLE_KEY=...
RESEND_API_KEY=...
EMAIL_FROM=...5. Deploy
Click "Deploy site" and Netlify will:
- Install dependencies
- Build your application
- Deploy to their CDN
Your app will be available at https://your-app.netlify.app.
Netlify Configuration
Create a netlify.toml in your project root:
[build]
command = "npm run build"
publish = ".next"
[build.environment]
NODE_VERSION = "20"
[[plugins]]
package = "@netlify/plugin-nextjs"Database Migrations
Netlify doesn't support running migrations during build. You have a few options:
- Run migrations manually before deploying
- Use a build plugin to run migrations
- Run migrations via API route (not recommended for production)
Custom Domain
To use a custom domain:
- Go to Domain settings → Add custom domain
- Follow DNS configuration instructions
- Update
NEXT_PUBLIC_SITE_URLenvironment variable
Functions Region
For better performance, select the region closest to your database:
- Go to Site configuration → Build & deploy → Functions
- Select the Functions region closest to your database
- Redeploy your site
Preview Deployments
Netlify automatically creates preview deployments for:
- Pull requests
- Branch pushes
- Merge commits
Each preview gets its own URL for testing.
Environment Variables by Context
Netlify supports different environment variables for:
- Production - Production deployments
- Deploy previews - Preview deployments
- Branch deploys - Branch-specific deployments
Troubleshooting
Build Failures
- Check build logs in the Netlify dashboard
- Verify Node.js version (set in
netlify.toml) - Ensure all dependencies are in
package.json
Function Timeouts
- Netlify Functions have a 10-second timeout on free tier
- Upgrade to Pro for longer timeouts
- Optimize your API routes
Database Connection Issues
- Verify
DATABASE_URLis set correctly - Check if database requires SSL (add
?sslmode=require) - Ensure database allows connections from Netlify's IPs
Environment Variables
- Verify variables are set in the correct context
- Redeploy after adding new variables
- Check for typos in variable names