nextjs/build-app

Build Next.js App

Compile and optimize the Next.js app for production deployment.

build
optimize
production
compile

Command

npm run build

Explanation

The `build` command runs several optimization steps: compiling TypeScript and JavaScript, bundling components, optimizing images, and generating static HTML pages. It creates a `.next/` folder with all production assets ready for deployment. Any build-time errors must be fixed before deployment.

Common Use Cases

  • Prepare app for deployment to Vercel or other hosting platforms
  • Generate optimized static pages for better performance
  • Catch build-time errors before production

Best Practices

  • Run `npm run lint` before building
  • Set NODE_ENV=production before build for accurate optimizations
  • Ensure `.env.production` variables are properly configured

Common Mistakes to Avoid

  • Running build before fixing lint or TypeScript errors
  • Missing environment variables required during build

Troubleshooting

Problem: Build fails with missing module error

Solution: Ensure all dependencies are installed and paths are correct.

Problem: Environment variable undefined

Solution: Declare variables in `.env.production` and prefix with `NEXT_PUBLIC_` if needed client-side.

Examples

Build the app for production

npm run build

Build using Yarn

yarn build