How I Deployed my React-on-Rails app to Heroku
Tags: react, reactonrails, railsapi
Published: 2019-08-27 16:14:30

---
title: How I Deployed my React-on-Rails app to Heroku
published: true
tags: react,react-on-rails,reactjs,rails-api
canonical_url: 
---

A space odyssey ….

![whos-who-congress-app](https://cdn-images-1.medium.com/max/1024/1*UInVHxDBimowrdiuJSzmaA.png)

I just graduated from the Flatiron School, and I’m pretty happy with my final project, a Congress Who’s Who app — an educational game. For the longest time, it was stuck on my computer. People who saw it wanted to know how they could use it. Could they download it? (Why yes, if you want to follow these 20 steps to create a Git account and fork and clone two repos, bundle them and run them on the right servers ….)

What I wanted to say was YES, here’s the link! And now I have one. My app lives here: [https://congress-front-end.herokuapp.com/](https://congress-front-end.herokuapp.com/).

If you’re having trouble deploying your React-on-Rails app to Heroku, especially if yours makes calls to an external API (application programming interface), read on, and learn from my mistakes!

Note: there are a few blog posts on how to do this, plus the Heroku documentation itself. Some parts of both worked for me, some didn’t. I’m this combination of steps, in this order, works for you!

### Assumptions (pre-requisites!)
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl1J9yApnGts8EXYoo%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fcbc-funny-comedy-l1J9yApnGts8EXYoo&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl1J9yApnGts8EXYoo%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="434" frameborder="0" scrolling="no"><a href="https://medium.com/media/255d34d133519651a4668449fec9e1b1/href">https://medium.com/media/255d34d133519651a4668449fec9e1b1/href</a></iframe>

This blog post assumes:

1. You have a working Rails API back-end.
2. Your Rails API back-end database is Postgres.
3. You have a working React front-end.
4. Both are tracked in separate Github repositories.
5. You have a Heroku account. (If not, follow[simple sign-up directions here](https://signup.heroku.com/))
6. You have installed the Heroku CLI (if not [follow these steps](https://devcenter.heroku.com/articles/heroku-cli#getting-started))

### PART ONE: Deploy your Rails API back-end

#### Step 1. Create a separate branch on Github for this effort.

My back-end was named congress-api. I created a new branch called deploy\_to\_heroku. After creation, github automatically moved me to my new branch.

```
// ♥ git co -b deploy\_to\_heroku
 
[**10:03:55**] (deploy\_to\_heroku) congress\_api

// ♥
```

Now, if everything goes wrong, you can at least revert back to your previous branch.

### Step 2. If this is your 2nd, 5th or … 20th attempt … clean up your previous mess.

_(skip this step if this is your first effort to deploy to Heroku)_

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3oKIPCSX4UHmuS41TG%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3oKIPCSX4UHmuS41TG%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3oKIPCSX4UHmuS41TG%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="420" frameborder="0" scrolling="no"><a href="https://medium.com/media/59b26ad2e0dd7cb3096055d9f96d810d/href">https://medium.com/media/59b26ad2e0dd7cb3096055d9f96d810d/href</a></iframe>

I tried and tried many times before I succeeded! During those previous efforts, I created and re-created the app on Heroku. Those previous efforts were done incorrectly and derailed my subsequent efforts. If you are in the same boat, go to your Heroku dashboard, and get rid of any previous attempts to create this particular app on Heroku. In my case, I had multiple copies of my app under different names.

AND … if along the way, you followed advice like I did to run a ‘yarn build’ of your project, you will need to delete your yarn.lock file from the root of your file tree. Heroku does not use yarn.lock.

### Step 3. Add a Procfile

What’s a procfile? It tells Heroku which commands to run to start your app!

Call it “Procfile” _with no extension and add it to your root directory._

I followed [Heroku’s documentation](https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#3-add-a-procfile) to the letter and made mine look exactly like their sample code for Rails:

```
web: bundle exec rails server -p $PORT
worker: bundle exec rake jobs:wor
```

### Step 4. Configure PUMA server

I followed [these steps](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server) from the Heroku documentation, but I am not sure they are enabled, since I do not call Puma in my Procfile (with a line like this `web: bundle exec puma -C config/puma.rb)

I’ll update this post when I have time to experiment with removing my Puma.rb file to see what happens.

### Step 6. Create a Heroku remote.

In terminal, navigate to your app’s root directory and [follow these instructions](https://devcenter.heroku.com/articles/git#creating-a-heroku-remote)from Heroku documentation.

### **Step 7. Add Heroku Postgres to your App**

[Follow these steps](https://devcenter.heroku.com/articles/heroku-postgresql#provisioning-heroku-postgres) from Heroku documentation

Then, in terminal run Heroku config. This will bring up the url for your new Heroku Postgres add-on. (it’s very long. I’m just showing the first part of mine below).

![](https://cdn-images-1.medium.com/max/694/1*cJi8a4YBCACFtwUQJpq_4g.png)

_Important!_

Grab that database url (“postgres:// etc”) and in your database.yml file, set DATABASE\_URL = \<”postgres:// etc”\>

### Step 6. Deploy!

From the Heroku documentation:

![](https://cdn-images-1.medium.com/max/1024/1*ndLgejXpSQRjliTU01bt3g.png)

Then navigate to the url for your new app. (If you can’t find it, go to your Heroku dashboard, click on the name of your new app, then click on “Open App” at the right. )

![](https://cdn-images-1.medium.com/max/1024/1*xR50fymEeG_zWO2O8pLC7A.png)

Grab that url, you will need it for Part Two!

### Step 7. See ….. nothing?
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FKOy9r0Fj1NKso%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FKOy9r0Fj1NKso%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FKOy9r0Fj1NKso%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="302" frameborder="0" scrolling="no"><a href="https://medium.com/media/2704ed6a06c6106b65eb5a0353d5b4c8/href">https://medium.com/media/2704ed6a06c6106b65eb5a0353d5b4c8/href</a></iframe>

Unless you found and followed steps to [import your Postgres database](https://devcenter.heroku.com/articles/heroku-postgres-import-export) as is, you deployed your app with an empty database like I did. So, after my ‘successful’ deploy, I was expecting to see the same thing I saw on my back-end page during development: lots of JSON data. Like so:

![](https://cdn-images-1.medium.com/max/1024/1*y5jnKJq5blAgiWFyMUHp6A.png)

But instead, I saw:

[]

Why? Because I deployed an app with an empty database.

If you aren’t making calls to an external database, you can fix this by running:

```
heroku rake db:create
heroku rake db:migrate
heroku rake db:seed
```

(Heroku does not support heroku rake:db reset.) [See documentation.](https://devcenter.heroku.com/articles/rake)

BUT, if your app makes calls to an external API, this won’t work! Why not? Read on …

### Step 8. If your app makes calls to an external API you must ….

_…use Heroku to configure envrionment variables._

OK, so this is the step that just about killed me. After doing lots of Googling and reading lots of documentation and Stack Overflow posts, I managed to get to the point where all I had to do was seed my database. But when I ran Heroku rake db:seed, I got a fail.

![](https://cdn-images-1.medium.com/max/1024/1*y5iWDd4-BozORbGih-lztg.png)

So the big hint was, check out /app/db/seeds.rb:9

What was there? The results of my API call, which by the way, worked perfectly in development.

![](https://cdn-images-1.medium.com/max/1024/1*OaCES2_AMDdzEXLaZIHnmQ.png)<figcaption>worked in development, why not in Heroku?</figcaption>

I tried sticking a pry into my production code, and could not hit it. Then, much Googling later, I discovered that Heroku does NOT recognize ENV variables, at least not in API calls. If you want to use your API key in an app you are deploying to Heroku, you must configure those variables in the Heroku CLI.

These, my friends, [are the magic steps.](https://devcenter.heroku.com/articles/config-vars#using-the-heroku-cli)

For me, they looked like this:

![](https://cdn-images-1.medium.com/max/836/1*VEBloW6mPs0cZC0hN6IX6Q.png)

etc.

After I got that straight, I ran heroku rake db:seed

and was THRILLED to see my database actually loading.

If you’re following along, take a break, do a little dance, and take heart! You have just finished the hardest part. Part Two is way way easier, I promise.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F4VUui5t6KxsJYXIbI8%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F4VUui5t6KxsJYXIbI8%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F4VUui5t6KxsJYXIbI8%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/d594902b7f42e974ad45508f599e87c5/href">https://medium.com/media/d594902b7f42e974ad45508f599e87c5/href</a></iframe>
### PART TWO: Deploy your React Front End

_(way easier than part one!)_

_I followed_ [_these directions from Shakhor Smith_](https://dev.to/smithmanny/deploy-your-react-app-to-heroku-2b6f) _(thx Shakhor!). The following is a distillation of his directions plus a few of my own observations and screenshots._

These instructions assume that you created your React front-end with create-react-app. Before you continue, go into terminal and navigate to the root directory of your React front end.

### **Step 1. Set up your front end to call the new url for your back end.**

During development, I ran my rails back end on http://localhost/3000. So all my fetches to my back end used http://localhost/3000 as a base URL. But with my back end actually up and running on Rails, I had to update those urls — um, all 10 of them.

When I set up those fetch calls, I typed the urls out like so:

fetch(http://localhost/3000/search/senators)

That’s fine for one or maybe two fetches, but I had more than 10. So to prepare for my Heroku deploy, I did what I should have done in the first place. I abstracted the base url into its own constant:

```
const API\_BASE\_URL="[http://localhost/3000](http://localhost/3000)"

...

fetch(API\_BASE\_URL+’/search/senators’)
```

I did that for 16 fetches in two files. (command D comes in handy! — select the first instance of a string, hit command D repeatedly to select subsequent instances, then type the replacement string, and voila!)

### Step 2. Figure out which versions of node and npm you are running.

Run npm -v

and node-v

![](https://cdn-images-1.medium.com/max/732/1*qxID1JWBpk9Tv28TWjgqlw.png)<figcaption>I’m running npm version 6.9.0 and node version 12.4.0</figcaption>

### Step 3. Add an engines object to package.json

![](https://cdn-images-1.medium.com/max/486/1*nsLvlE5blfralsAm1jkWPA.png)

following Shakhor’s lead, I put mine right above dependencies (I don’t know if order matters, let me know if you know it does!)

![](https://cdn-images-1.medium.com/max/830/1*86NQRMTvAbWcR2WQTtKtgg.png)

### Step 4. Create a remote Heroku repository the easy way.

Open up your Heroku dashboard in your browser. Click ‘New’ and select “Create new App”

![](https://cdn-images-1.medium.com/max/1024/1*YYTQofKRZuknfLO1Mq3tfA.png)

You’ll see a dashboard for the app you just created. Go to the deploy tab and follow directions to connect your app to Github, enable automatic deploys, and choose the branch you are deploying (in my case it was deploy\_to\_github). Don’t forget to click the deploy branch when you are done!

If you get fail message the deploy tab, plus a log of what went wrong. The messages are very clear, and include clear instructions on how to fix what went wrong.

My deploy failed because of conflicting yarn.lock and package.json file; the error message clearly told me to delete yarn.lock. Then you have to save that change and re-run git push heroku master to push that change to Heroku.

After fixing two such errors, success! I finally got my React and Rails App running on Heroku!!!

Here’s hoping this reduces your deployment time to less than one hour!!!