Create React App, Heroku - Demo
This is the create react app live on heroku using node.js and express server.
- Deploy a create react app to Heroku.
- React (version 16.2.0)
- Create React App
- Heroku Set up a free account
- NPM (version 5.6.0)
- Node (version 6.10.3)
- express (version 4.16.3)
- First
npm install
then runnpm start
. (may need to have npm version 5.2+ installed on your machine)
- Go to Create React App and follow the instructions in the readme.
cd app-name
into appnpm run-script build
to build the app in a build directory.- Create an
app.js
filetouch app.js
. - For Heroku this app points to the
app.js
server which uses node.js and express. (node.js is required to serve locally)
//app.js
const express = require('express');
const http = require('http');
const path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'build')));
const port = process.env.PORT || '8080';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost:${port}`));
- Run
node app.js
to serve the app athttp://localhost:8080
. - Run
touch Procfile
in this app's root specifies the server for heroku to use.
//Procfile
web: node app.js
- Run
npm install express --save
. - A few adjustments to
package.json
are necessary for the heroku deploy process.postinstall
andengines
are important parts ofpackage.json
for a successful deploy.
//package.json
{
"name": "app-name",
"version": "0.1.0",
"private": true,
"main": "app.js",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"postinstall": "react-scripts build"
},
"engines": {
"node": "~6.10.3",
"npm": "~5.6.0"
},
"dependencies": {
"express": "^4.16.3",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.1",
}
}
- Assuming the heroku account is created and heroku toolbelt is installed make an initial commit and push to a github repo, then login to heroku, create a heroku app and push to heroku. Steps as follow.
git commit -am "initial commit"
thengit push
. (pushing app to new github repo)- Login to heroku
heroku login
enter heroku password. - Create heroku app
heroku create app-name
. (this will add the heroku remote) - Deploy to heroku
git push heroku master
.
There may be an error during deploy pertaining to package.lock
and yarn.lock
. Delete yarn.lock
from the app, commit and push to github repo and then git push heroku master
again and the error will go away.
Each time there is a change to the app commit and push to github then git push heroku master
to keep the two master branches in sync.