Sikau

Next.js · Day 18 · about 25 min

Environment variables and deploying

Secrets, build and ship.

From laptop to the world

Your app works locally. Shipping it means keeping secrets out of code, building an optimised version, and running it somewhere people can reach — with the right settings for each environment.

Env vars, build, deploy

# .env.local (never committed)
DATABASE_URL=postgres://user:[email protected]/cafe
NEXT_PUBLIC_MAP_KEY=pk_live_123

# terminal
npm run build   # optimised production build
npm run start   # run it
  • Vercel (made by the Next.js team): connect your Git repo; every push deploys, every pull request gets a preview URL.
  • Self-hosting: next build && next start on any Node.js server, or the output: "standalone" Docker setup.
  • Set environment variables in your host's dashboard, separately for preview and production.
  • Use next/image for responsive, lazy-loaded images and next/font for self-hosted fonts with no layout shift.

The rest of this lesson

  • Server-only and public variablesexplain
  • What happens at build timeexplain
  • Safe?predict
  • Server or public?match
  • Shipping a changeorder
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 19 lessons of Next.js