Deployment of React Application on Heroku is very simple task. We can deploy React app on Heroku by many ways like Heroku CLI and Github.
Before deployement process we need to sure that our application should be in a production deployment mode.
Suppose we have created the build
folder for production deployment. If still you have not created the
build or deployment folder then we can create it by following cammands.
If you are using webpack then npm run build
is the command for create the build files. We need to
deploy only these build files to run our reat app with any backend server like express, hapi and other web servers.
If your application structure is like below format after create the build files.
/build /node_modules /public /src package.json README.md
To deploy the React application we need a web server like express js.
So we need to install a express js framework to deploy the react app.
Create a package.json
and server.js
file to with express js framework.
package.json
server.js
const express = require('express'); const app = express(); const path = require('path'); app.use(express.static(__dirname + '/build')); app.listen(process.env.PORT || 8080); app.get('/*', function(req, res){ res.sendFile(path.join(__dirname + '/build/index.html')); }); console.log('Server listening');
Now upload all these files on Github or use Heroku CLI to deploy directly. Here we will use git to upload all the files on github
/build package.json server.js
Then we can deploy our React app with heroku. We need Heroku account and need to create our app with Heroku.