Deploying a fullstack React app to Heroku
A few steps that may come in handy when deploying your fullstack React app.
Deployment Steps
1. Update Server.js
Server.js We will create a static build folder for the front end. This will be your React app. We need to tell the server where this will be so in your server.js file, add this:
//server.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
2. Build
Run the yarn command from your root folder:
yarn run build
3. Add build folder to git
Now we have to add that build folder to your git so Heroku has access to it.
git add client/build
git commit -m 'Adding staticbuildfolder'
4. Heroku
Create your Heroku app. Note, you need to install the Heroku toolbelt (https://blog.heroku.com/the_heroku_toolbelt)
heroku apps:create NAME_OF_APP
Now you need to push your app to Heroku.
git push heroku master
5. Connecting your database
To connect to your database, you will have to provision Heroku with that database.
For Mongo, I tend to use MongoLab: https://devcenter.heroku.com/articles/mongolab
For SQL, you can use ClearDb. https://devcenter.heroku.com/articles/cleardb
You will have to update your connection string in your server to make this wok.
Troubleshooting
To view the Heroku logs use
heroku logs --tail
Other common issues you may run into:
- .lock files You have two .lock files (packagejson.lock and yarn.lock). Heroku only allows for one so you can simply delete one.
- dependencies You are missing dependencies. Heroku will let you know if you look at the logs.
- .gitignore Make sure your client/build folder is not in your .gitignore file.
- A great reference: https://github.com/fullstackreact/food-lookup-demo
I hope you enjoyed this installment of Zack knows some stuff. I'm a developer, founder, lecturer
https://facebook.com/ransomly
https://instagram.com/positivelyZack
https://instagram.com/ransomly_inc
Leave Deploying a fullstack React app to Heroku to:
Read more #react posts
Best Posts From positivelyzack
We have not curated any of positivelyzack's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From positivelyzack
- Adding props or functionality to React children
- Positive Product Design
- Half Life: The Decay of Utility of Software Knowledge or why you should learn to learn
- Pure Functions
- Deploying a fullstack React app to Heroku
- Components with Props In React
- The structure of app created with the create-react-app tool.
- ES6 Javascript exports, imports and the difference between 'default export' & 'export'