Deploying a React App
React is a popular JavaScript library for building user interfaces, developed and maintained by Facebook. It enables developers to create fast, interactive, and reusable UI components for web applications and SPAs.
This guide explains how to deploy a React application to Klutch.sh, both with and without a Dockerfile. It covers installation, sample code, and deployment steps.
Prerequisites
- Node.js & npm installed (Download)
- Git and GitHub account
- Klutch.sh account
Getting Started: Create a React App
- Create a new React app using Create React App:
Terminal window npx create-react-app my-react-appcd my-react-app - Start the development server:
Your app should be running at http://localhost:3000.
Terminal window npm start
Sample Code (src/App.js)
Replace the contents of src/App.js
with:
function App() { return ( <div> <h1>Hello from React on Klutch.sh!</h1> </div> );}export default App;
Deploying Without a Dockerfile
React apps are static sites after build. Klutch.sh can deploy static sites directly.
- Push your React app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project.
- Create a new app:
- Select your React GitHub repository and branch
- Choose “Static Site” as the app type
- Set build command:
npm run build
- Set output directory:
build
- Configure region, compute, and environment variables as needed
- Click “Create” to deploy. Klutch.sh will build and serve your static React app.
Deploying With a Dockerfile
If you need custom server logic or want to use a Dockerfile:
- Add a
Dockerfile
to your project root. Example:# Build stageFROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .RUN npm run build# Production stageFROM nginx:alpineCOPY --from=builder /app/build /usr/share/nginx/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, create a new app and select the Dockerfile option when prompted.
- Set service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and serve your React app via Nginx.
Resources
Deploying React to Klutch.sh is fast and flexible. Use static site deployment for most cases, or Dockerfile for custom setups.