Complete Guide to Running Production Builds with Create React App

Nov 26, 2025 · Programming · 11 views · 7.8

Keywords: Create React App | Production Build | Static Server | Deployment | npm run build

Abstract: This article provides a comprehensive guide on creating and running production builds with Create React App. It explains the purpose of the npm run build command, which generates optimized production files in the build directory. The focus is on using the serve static server to run production builds, including installation, server startup, and application access. Alternative approaches using Express custom servers are also covered, along with special handling requirements for client-side routing. The article concludes with an overview of other deployment options and common issue resolutions, offering developers complete guidance for production environment deployment.

Understanding Production Builds

When developing React applications with Create React App, the npm run build command creates an optimized production version. This command packages the entire application into the build directory, generating compressed and optimized files suitable for production deployment.

Using the Serve Static Server

The simplest method to run a production build is using the serve package. First, install it globally:

npm install -g serve

After installation, run the following command from your project root:

serve -s build

This command starts a static file server, typically serving on port 3000. The console will display the access address, usually http://localhost:3000, where you can access the production version of your application.

Custom Port Configuration

To use a different port, employ the -l or --listen parameter:

serve -s build -l 4000

This will run the application on port 4000. To view all available options, run serve -h.

Custom Server with Express

As an alternative to serve, you can create a custom server using Express:

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'build')));

app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(9000);

This configuration not only serves static files but also handles client-side routing redirection, ensuring all routes return index.html.

Special Handling for Client-Side Routing

If your application uses client-side routing based on HTML5 pushState (such as React Router's browserHistory), special configuration is required to ensure all routes correctly return index.html. In the Express example above, we use app.get('/*') to capture all route requests.

Other Deployment Options

Beyond local execution, production builds can be deployed to various cloud platforms:

Build Configuration Optimization

For specific deployment environments, customize build configurations using environment variables. Create a .env.staging file for environment-specific variables and add a build script to package.json:

{
  "scripts": {
    "build:staging": "env-cmd -f .env.staging npm run build"
  }
}

Relative Path Building

If your application needs to be deployed under a subpath, set the homepage field in package.json:

"homepage": "http://mywebsite.com/relativepath"

Or use relative paths:

"homepage": "."

Common Issue Resolution

Various issues may arise during deployment:

Performance Optimization Recommendations

While production builds include many optimizations, further improvements are possible:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.