Not generating a production build before deployment

Loading

The Importance of Creating a Production Build Before Deployment

Deploying a React application without generating a proper production build is a common mistake that leads to performance issues, security vulnerabilities, and unexpected behavior in production.

Why You Must Create a Production Build

  1. Performance Optimization: Minified code, optimized assets, and code splitting
  2. Smaller Bundle Size: Typically 3-10x smaller than development bundles
  3. Removed Development Tools: Excludes React warnings, dev tools, and source maps
  4. Proper Environment Variables: Uses production-specific configurations
  5. Optimized Static Assets: Compressed images and media files

How to Properly Create a Production Build

1. Standard Create React App Build

# ✅ Correct build command
npm run build

This creates an optimized build/ folder with:

  • Minified JavaScript and CSS
  • Hashed filenames for cache busting
  • Production-ready HTML files
  • Compressed assets

2. Verifying the Build

# Test your production build locally
npx serve -s build

# Or with a specific port
npx serve -s build -l 4000

3. Advanced Build Options

# Analyze bundle size
npm run build -- --profile
npx source-map-explorer 'build/static/js/*.js'

# Generate stats for analysis
npm run build -- --stats

Deployment Checklist

  1. Run the build script
   npm run build
  1. Verify the build folder
  • Check build/index.html exists
  • Confirm static assets are present
  1. Test locally
   serve -s build
  1. Deploy the build folder
  • Only deploy the contents of build/, not the entire project

Common Mistakes to Avoid

1. Deploying Source Code Instead of Build

# ❌ Wrong - deploying entire project
git push production main

# ✅ Correct - only deploy build folder
npm run build
scp -r build/* user@server:/var/www/html

2. Forgetting to Install Dependencies

# ❌ Wrong - building without dependencies
npm install --omit=dev
npm run build

# ✅ Correct - install all dependencies first
npm install
npm run build

3. Ignoring Build Warnings

# Always check for build warnings and fix them
npm run build

Production Build vs Development Mode

FeatureDevelopment BuildProduction Build
Bundle SizeLarge (~MBs)Small (~100s KB)
PerformanceSlowOptimized
Error MessagesDetailedGeneric
Source MapsIncludedExcluded (unless enabled)
React DevToolsEnabledDisabled

Best Practices

  1. Automate builds in your CI/CD pipeline
  2. Test the production build before deploying
  3. Set memory limit for large apps
   export NODE_OPTIONS=--max_old_space_size=4096
   npm run build
  1. Use HTTPS in production
  2. Implement proper caching headers
  3. Monitor bundle size regularly

Troubleshooting Build Issues

  1. Memory errors:
   # Increase Node.js memory limit
   export NODE_OPTIONS=--max_old_space_size=4096
   npm run build
  1. Missing environment variables:
   # Ensure .env.production exists
   cp .env .env.production
   npm run build
  1. Build failures:
   # Clean and rebuild
   rm -rf node_modules build
   npm install
   npm run build

Remember that deploying without a proper production build will result in significantly worse performance and potential security issues. Always build before deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *