Capítulo 432 de 456

next CLI

Core Idea

Reference for the next CLI: dev, build, start, info, telemetry, typegen, upgrade, and experimental-analyze commands.

Key Concepts

  • Commands: dev, build, start, info, telemetry, typegen, upgrade, experimental-analyze. Running next with no command aliases to next dev.
  • next dev: dev server with HMR; outputs to .next/dev (separate from .next, so dev and build can run concurrently). Key flags: --turbopack (default), --webpack, -p/--port, -H/--hostname, --experimental-https(-key/-cert/-ca), --experimental-cpu-prof.
  • next build: production build; key flags: --turbopack/--webpack, -d/--debug, --profile, --no-mangling, --experimental-app-only, --debug-prerender, --debug-build-paths=<patterns>.
  • next start: runs production build (requires prior next build); --keepAliveTimeout for downstream proxy tuning.
  • next info: prints OS/binaries/package versions for bug reports; --verbose for more detail.
  • next typegen: generates route TypeScript definitions without a full build; output to <distDir>/types; also regenerates next-env.d.ts.
  • next upgrade: upgrades the app to latest/specified version via --revision <revision>.
  • next experimental-analyze: analyzes bundle output via Turbopack; -o/--output writes static files to .next/diagnostics/analyze without starting a server; no build artifacts produced.

Code Examples

next build --debug-build-paths="app/**/page.tsx,!app/admin/**"
  • O que demonstra: builda só rotas específicas via glob, excluindo um subset com !.
next typegen && tsc --noEmit
  • O que demonstra: gera tipos de rota e valida com TypeScript sem rodar build completo, útil em CI.
NODE_OPTIONS='--inspect' next
  • O que demonstra: repassa argumentos Node.js pra qualquer comando next.

Reference Tables

CommandDescription
devDev mode with HMR, error reporting.
buildOptimized production build; shows per-route info.
startProduction mode (needs prior next build).
infoSystem details for bug reports.
telemetryEnable/disable anonymous telemetry.
typegenGenerate TS route definitions without full build.
upgradeUpgrade to latest/specified Next.js version.
experimental-analyzeAnalyze bundle output via Turbopack (no build artifacts).
VersionChanges
v16.1.0Added next upgrade and next experimental-analyze.
v16.0.0Removed JS bundle size metrics from next build output.
v15.5.0Added next typegen.
v15.4.0Added --debug-prerender for next build.

Anti-patterns

  • Deploy com --debug-prerender: desativa minificação server-side e gera source maps, impactando performance; só pra debug local.
  • Setar PORT via .env: não funciona, o servidor HTTP sobe antes de qualquer código ser inicializado; use -p ou variável de ambiente na chamada.

Key Takeaways

  1. next dev/next build podem rodar simultaneamente porque escrevem em pastas diferentes (.next/dev vs .next).
  2. Com npm run, use -- antes das flags do CLI pra repassar corretamente (não necessário com pnpm/yarn/bun).
  3. --experimental-cpu-prof grava perfis .cpuprofile em .next-profiles/, abríveis no Chrome DevTools.
  4. next typegen roda a config de produção; garanta env vars/dependências disponíveis pra config carregar.

Connects To

  • create-next-app CLI (ch431): cria o projeto que este CLI opera.
  • TypeScript (ch428): typedRoutes depende de next typegen/next dev/next build.
  • ESLint (ch429): next lint foi removido; use ESLint CLI diretamente em vez de um subcomando next.