- A Heroku account (it's free to sign up!)
- Heroku CLI installed on your machine
- Node.js and npm (or yarn) installed
- Git installed
- A working Node.js and React application
So, you've built this awesome full-stack application using Node.js and React, and now you're probably wondering, "How do I get this thing live so the world can see it?" Well, you've come to the right place! This guide will walk you through deploying your Node.js React app to Heroku, step by step. Heroku is a fantastic platform as a service (PaaS) that makes deploying web applications relatively painless. Let's dive in!
Prerequisites
Before we get started, make sure you have the following:
Step 1: Setting Up Your React Frontend
First, let's focus on the React frontend. We need to make sure it's set up to be served correctly from our Node.js backend once deployed on Heroku. Here's how:
Create a Production Build
Navigate to your React application's directory in the terminal and run the following command:
npm run build
Or, if you're using yarn:
yarn build
This command creates an optimized production build of your React application in a build directory. This directory contains all the static assets needed to run your frontend.
Configure package.json for Deployment
Open your React application's package.json file and add a homepage field. This tells React where the app will be deployed. If you're deploying to the root domain, set it like this:
"homepage": "."
If you're deploying to a subdirectory, specify the full URL:
"homepage": "https://yourdomain.com/your-subdirectory"
The homepage field is crucial because it ensures that all the paths to your static assets are correct when the application is deployed. Without it, you might encounter issues with broken links and missing files.
Also, ensure you have a start script in your package.json. This script will be used by Heroku to start your application. If you don't have one, add it like this:
"scripts": {
"start": "node server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Make sure the start script points to the correct entry point of your Node.js server (e.g., server.js, app.js, or index.js).
Step 2: Setting Up Your Node.js Backend
Now, let's configure the Node.js backend to serve the React frontend. This involves setting up a simple Express server to serve the static files from the build directory.
Install Express
If you haven't already, install Express in your Node.js project directory:
npm install express --save
Or, with yarn:
yarn add express
Express is a lightweight web application framework for Node.js that provides a robust set of features for building web applications and APIs.
Create a Server File
Create a file named server.js (or any name you prefer) in the root of your project. This file will contain the code for your Express server.
Configure the Express Server
Open server.js and add the following code:
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 5000;
// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));
// Put all API endpoints under '/api'
app.get('/api/hello', (req, res) => {
res.send('Hello from Express!');
});
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Explanation:
express.static(path.join(__dirname, 'client/build')): This line tells Express to serve static files (HTML, CSS, JavaScript, images) from thebuilddirectory. Make sure the path points to the correct location of your React app's build directory. In this example, it assumes your React app is in a folder named "client".app.get('/api/hello', ...): This is an example API endpoint. You can define your own API endpoints here to handle data requests from your React frontend.app.get('*', ...): This is a catch-all route that serves theindex.htmlfile from thebuilddirectory for any route that doesn't match an API endpoint. This is important for single-page applications (SPAs) like React apps because it allows the client-side routing to handle navigation.process.env.PORT || 5000: This sets the port that the server will listen on.process.env.PORTis an environment variable that Heroku sets when the application is deployed. If this variable is not set (e.g., when running locally), the server will listen on port 5000.
Update package.json for Heroku Deployment
Add a postinstall script to your package.json file. This script will run automatically after Heroku installs your application's dependencies. It's used to build your React application.
"scripts": {
"start": "node server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postinstall": "cd client && npm install && npm run build"
}
Explanation:
cd client: This changes the directory to your React application's directory (assuming it's named "client").npm install: This installs the dependencies for your React application.npm run build: This creates a production build of your React application.
Also, specify the Node.js version in your package.json file. Heroku needs this to know which version of Node.js to use. Add the following to your package.json:
"engines": {
"node": "16.x"
}
Replace `
Lastest News
-
-
Related News
Dante Bini: Innovative Architecture And Его Visionary Domes
Alex Braham - Nov 9, 2025 59 Views -
Related News
OSCCIMBS Bank Cambodia: Locations & Essential Info
Alex Braham - Nov 12, 2025 50 Views -
Related News
OSC Loans: Your Financing Journey In Pictures
Alex Braham - Nov 12, 2025 45 Views -
Related News
Carnifex's Dark Heart Ceremony: A Live Metal Experience
Alex Braham - Nov 14, 2025 55 Views -
Related News
South African Sportswear: Style & Performance
Alex Braham - Nov 13, 2025 45 Views