mogwai
Posted on June 6, 2020
Heroku is easy to use, until it isn't. The first day we - a bunch of full stack developers-in-training - were introduced to Heroku in class, we were blown away.
'That's it?' we asked, bewildered.
'That's it!' laughed our instructor. It was the simplest class ever. Click here, tap here, grant permission there and you have a continuously deployed server. Whew. We took the day off and had fun for the first time in a long time.
Those beautiful days are long gone now, and in the real world, you'll discover that when Heroku fails, it does so in passively tyrannical ways. It is terser than your room mate. Today, I'm going to run through a list of Heroku errors I've encountered so far and how I solved them. Hopefully this article will save you a trek through several Stack Overflow pages.
First, let's keep the main thing the main thing
Don't forget to process.env.PORT all the things
As you probably know, your local environment uses a hard-coded port number to run your local servers. Things like 'localhost:5000' for example are great on your local machine, but if you leave that hard-coded port in your server, it will become a code H10 HEROKU problem.
As a rule, if you must use port 5000 locally, always use a logical OR to add the heroku environment.
`const PORT = process.env.PORT || 5000;
That should do it.
Why is this necessary? Heroku sets its own port at production time, and if you override it with your local configurations, it will throw up it hands in annoyance and quit on you.
The notorious Heroku logs
Sometimes you'll encounter a problem where you'll get a code H10 error that says 'MODULE_NOT_FOUND' without seeing what the module is. I'm not sure why, but it often feels like the heroku logs are incomplete.
This can be a pain in the ass to debug. What module is not found? Where?! These are the questions!
To fix this one, a great thing to do is to open the heroku cli on your favorite terminal.
Type in heroku run bash
then npm start
(if you're using Node js - basically start your server). It will throw the same error as before, but this time more explicitly. Note the errors, exit bash by typing exit
, implement your fixes, do a git push heroku master
and do a heroku restart
and heroku open
and things should be much better.
Procfile
You typically don't need to manually add a Procfile, since Heroku can read your package.json file to tell where the server file is located. However, sometimes the odds aren't in your favor (usually your fault), and now you explicitly need to tell Heroku where to look for your server.js or index.js or app.js file.
Whatever the case, this is where you create a Procfile (no extensions - it's like gitignore like that).
In the Procfile, you'll tell heroku how to find your file, eg:
web: src/index.js
HINT: You need a Procfile if Heroku says something like 'cannot find /path/to/file.js'
That's all for now. I hope this helps at least one person!
Posted on June 6, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.