I realized recently that too much of my monthly Azure spending was devoted to "toy" websites. I wanted these apps deployed online, but I didn't need the sites to be particularly fast.
As a result, I decided to move a few of my existing Node/Express apps to Heroku (Heroku offers free hosting for basic websites, but also provides easy scaling, so if you need more from your app, you can simply start paying a monthly fee).
This article will outline exactly what you need to deploy a Node application to Heroku (with Grunt and Bower)
Step 0: Follow Heroku's Node Tutorial
Deploying a Node app to Heroku is already covered in great detail on Heroku's website. To start, simply follow their tutorial (they make it really easy to get started).
Step 1: Add Grunt and Bower as dependencies to your Node app
If you followed the tutorial linked above, most of the hard work should be done. At this point, the next step is to open up a terminal, navigate to your app's directory, and type the following: npm install --save bower grunt
Step 2: Add a postinstall script to run after deployment
Open your package.json
file. Note that it now lists grunt
and bower
under "dependencies" (this is necessary because Heroku by default runs npm install --production
, which will not install dev dependencies (normally, you might put grunt or bower under dev dependencies).
Now, with your package.json
still open, you should see a section of the file labeled "scripts". Change it to look something like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"postinstall": "bash ./post_install.sh"
},
The "start"
part should already be there after completing the Heroku-provided tutorial, and your "test"
line may or may not be different depending on whether you added automated testing to your app.
So the important part here is the "postinstall"
line. All this is saying is: after Heroku installs your dependencies, it should run that script. In this case, it's specifically saying: after Heroku installs your dependencies, it should run the bash script called post_install.sh
Step 3: Write the post_install.sh bash script
Now that we're telling Heroku to run a script called post_install.sh
, we need to create that script.
So, in your app's directory (in the same directory as your package.json file), create a file called post_install.sh
, and write in the following:
#!/bin/bash
./node_modules/bower/bin/bower install
./node_modules/grunt-cli/bin/grunt
Step 4: Commit and push
That's it! The scripts
section of the package.json
file tells Heroku to run a script called post_install.sh
, and that script has instructions to run both bower install
and grunt
just as if you had done it manually from the command line.
The result is that your new app will deploy to heroku, install front-end components, then build your app using Grunt.
(Note: clever readers may notice that this method can be used to run really ANYTHING after the app deploys--all you need to do is add whatever commands you like to the post_install.sh
script)