Deploying an Angular Universal App to Heroku
tl;dr — view/clone/install the ready-made repo here: https://github.com/stephengardner/universal-starter-heroku
Angular Universal is officially the newest, coolest, stable thing to hit the Angular platform. It’s finally received support and compatibility with major frameworks like the Angular flex-layout
system, and if we have the option to adopt it, I can’t find any reason why not.
To some, however, the idea of “universal” — or server-side — rendering is simply just another piece to an already complicated puzzle. It’s another potential break-point of debugging claustrophobia in their full-stack application.
Developing a Universal app does have it’s own set of “gotchas”. They’re even listed in the official Universal Starter repository. But it doesn’t have to be a grim experience. And for those familiar with the ease of quick prototyping and scalable production builds using Heroku, the process can stay super simple.
Deploying a Universal Starter app to Heroku can be accomplished in just a few clicks, we first need a to make a few necessary edits to the original Universal Starter repo, which I’ll explain here. Don’t be scared, they’re not that difficult, and they don’t change the underlying structure of the app.
Looking for a shortcut?
Before I begin, you can find my repo with all of these changes made already, here: https://github.com/stephengardner/universal-starter-heroku
Installing the Universal Starter Repo
You can grab the Universal Starter Repo here, or do the following from the command line:
git clone https://github.com/angular/universal-starter universal-starter-heroku
cd universal-starter-heroku
npm i
Making the changes to Universal Starter
- Removing
yarn.lock
.
This first change we need to make is due to the fact that Universal Starter has both apackage-lock.json
and ayarn.lock
file. If we don’t remove one of these, then Heroku will complain with the error:Two different lockfiles found: pacakge-lock.json and yarn.lock
If you prefer to use yarn over npm, I don’t blame you, but for the sake of this tutorial we’re just going to stick withnpm
, and we’ll remove theyarn.lock
. Yep, just delete it. - Add a
Procfile
.
What is a Procfile? In super-simple terms, it’s just a file that tells Heroku what command-line string to execute in order to start up your app. You can have multiple “processes” (hence “Proc”-file), but Heroku comes standard with 1 web process. So for this basic setup, we have a Profile that will tell Heroku “Hey, Heroku. Do this when you start up thatweb
process”.
How to create a Procfile?
The same way you create any file, just name itProcfile
with no extension, none at all.
What goes in the Procfile?web: npm run start:heroku
That’s it. Close that file, now, we’re done with it. - Next, edit the
package.json
to create ascript
command that we’re going to need.
Add a line under thescripts
object of ourpackage.json
and put in the following code:“start:heroku”: “node dist/server”
.
This command will eventually start the node server that will be built by Angular Universal. - Once again go ahead and edit the
package.json
’sscripts
object and create apostinstall
script.
What is a postinstall script?
A “postinstall” script is a script that is run automatically by Heroku afternpm
has finished installing all the dependencies on each build.
In this case, we want to make sure that ourdist
server is built and ready for deployment by Heroku, so what we can do is kick off thebuild:ssr
command on Heroku’s end, after Heroku has received the code.
How do I create it?
To do this, add another line under the “scripts” object of ourpackage.json
file and put in the following code:“postinstall”: “npm run build:ssr”
That’s it, your changes are all made, now the fun stuff!
Deploying to Heroku
git init
heroku login
heroku create
git add .
git commit -m "initial commit"
git push heroku master
Wait a moment for the push to complete. Remember that we’re running build:ssr
on Heroku’s end, so it might take a minute or two.
Now run heroku open
, and you will see your freshly deployed Angular Universal app on Heroku.
Hope this helped :)
If you have any questions, reach out in the comments and I’ll be happy to explain further.
❤️ Augie