Skip to main content
General
Deployment

Netlify

Learn how to deploy your application to Netlify.

Open MarkdownFull AI corpusFeedback

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

  1. Click "Add new site" → "Import an existing project"
  2. Connect your Git provider (GitHub, GitLab, or Bitbucket)
  3. 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 with Prisma, the build command already includes Prisma generation:

  • Build command: npm run build (includes prisma generate)

4. Add Environment Variables

Add the deployment variables. DATABASE_URL and BETTER_AUTH_SECRET are required. Add the Stripe and Resend variables when those features are enabled:

  1. Go to Site settings → Environment variables
  2. Add variables from your .env:
Netlify Environment Variables
DATABASE_URL=postgresql://...
BETTER_AUTH_SECRET=...
NEXT_PUBLIC_SITE_URL=https://your-app.netlify.app
STRIPE_SECRET_KEY=...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=...
STRIPE_WEBHOOK_SECRET=...
NEXT_PUBLIC_STRIPE_PRICE_PRO_MONTHLY=price_...
NEXT_PUBLIC_STRIPE_PRICE_PRO_YEARLY=price_...
NEXT_PUBLIC_STRIPE_PRICE_LIFETIME=price_...
NEXT_PUBLIC_STRIPE_PRICE_CREDITS_STARTER=price_...
NEXT_PUBLIC_STRIPE_PRICE_CREDITS_BASIC=price_...
NEXT_PUBLIC_STRIPE_PRICE_CREDITS_PRO=price_...
RESEND_API_KEY=...
EMAIL_FROM=...

5. Deploy

Click "Deploy site" and Netlify will:

  • Install dependencies
  • Generate Prisma Client
  • 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:

netlify.toml
[build]
  command = "npm run build"
  publish = ".next"

[build.environment]
  NODE_VERSION = "22.21.1"

[[plugins]]
  package = "@netlify/plugin-nextjs"

Database Migrations

Netlify doesn't support running migrations during build. You have a few options:

  1. Run migrations manually before deploying
  2. Use a build plugin to run migrations
  3. Run migrations via API route (not recommended for production)

Custom Domain

To use a custom domain:

  1. Go to Domain settings → Add custom domain
  2. Follow DNS configuration instructions
  3. Update NEXT_PUBLIC_SITE_URL environment variable

Functions Region

For better performance, select the region closest to your database:

  1. Go to Site configuration → Build & deploy → Functions
  2. Select the Functions region closest to your database
  3. 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
  • Make sure Prisma Client is generated: npx prisma generate

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_URL is 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