Course contentsShow
← React
Lesson 47 of 2,3962. Setup and Modern ToolingFree lesson

Previewing the Production Build

Use 'npm run preview' to test the built app locally before deploying to catch build-only issues.

Previewing the Production Build

What you'll learn: How to test your production-ready React app locally before deploying it to catch build-specific issues.

Why Preview Matters

You've just run npm run build and created an optimized production bundle. But here's the problem: your development environment (npm run dev) behaves differently than production. Features like HMR, source maps, and development warnings won't exist in production. Some bugs only appear after the build process optimizes your code.

Think of it like test-driving a car after assembly. You wouldn't ship a vehicle straight from the factory floor without checking if everything works together properly. Similarly, you shouldn't deploy your built app without testing it first.

The Preview Command

Vite provides a simple command to serve your production build locally:

npm run preview

This command:

  • Serves the contents of your dist folder (created by npm run build)
  • Runs a local server similar to production hosting
  • Shows you exactly what users will experience

What to Check

When previewing, look for:

  • Broken imports: Sometimes relative paths work in dev but fail in production
  • Missing assets: Images or files that didn't copy correctly to dist
  • API endpoint issues: Environment variables that differ between dev and production
  • Route problems: Client-side routing that works in dev but breaks on static hosts

The Workflow

  1. Make changes to your code
  2. Run npm run build
  3. Run npm run preview
  4. Test thoroughly in your browser
  5. Fix any issues, then repeat

Key Takeaway: Always preview your production build locally with npm run preview before deploying—it catches build-specific bugs that development mode hides.