Skip to content

Deploying with Heroku

Gabriel Lebec edited this page Sep 19, 2016 · 27 revisions

Deployment with Heroku should be streamlined, depending on the functionality and needs of your application. Before attempting to deploy, it is suggested that you read Getting Started with Node.js on Heroku.

To deploy, you should be able to follow these steps.


Note:

  • Your project should already be a git project.

  • All listed commands should be run in your project root.

  • If your application requires extra processes or tasks, your mileage may vary.


  1. Ensure you have a heroku account authenticated on your machine. Read Getting Started with Node.js on Heroku if you have not already.

  2. heroku create

  • This will generate a heroku application for your project. git remote show should now list heroku as a remote repository for your git project.
  1. heroku config:set NODE_ENV=production
  • This command will tell heroku to start your application with an environment variable NODE_ENV equal to production. This is read in server/env/index.js in order to select the server/env/production.js file for application information.
  1. heroku config:set SESSION_SECRET=anythingyouwant
  • This is the second and last configuration requirement for your application to run. Much like the previous step, this sets the environment variable in your application when heroku runs it. This variable is accessed inside of server/env/production.js.
  • Optional: Any other environment variables being used--most likely third-party authentication credentials--need to be set in a similar fashion to the above. Reference the environment variable files.
  1. heroku addons:create heroku-postgresql:hobby-dev
  • This will also set the DATABASE_URL environment variable.
  • IMPORTANT - for 1607, your server/env/production.js currently checks process.env.DATABASE_URI for the location of the database url, but Heroku-Postgres creates the DATABASE_URL variable instead. You will need to change the string process.env.DATABASE_URI to process.env.DATABASE_URL — keep everything else, including the key DATABASE_URI:, the same.
  1. git push heroku master
  • Ensure you have something to push.
  • This will deploy your code to heroku. This will be the way you redeploy your application once changes are made.
  • This will likely take a while the first time as npm install runs for the first time.
  1. heroku open
  • Simply opens your application's heroku URL in your browser. It should work! Congrats!
  • If it doesn't work:
    • Run heroku config and ensure that DATABASE_URL is set, SESSION_SECRET is set and NODE_ENV is set to production.
    • Make sure your package.json has the correct start script. Heroku uses this command to start your process.
    • Make sure your package.json has the correct postinstall script. This should resemble gulp build by default.
    • Your application may require extra steps to successfully deploy. These extra steps are application-specific and can not be addressed in this walkthrough.

Once you've deployed, you can re-deploy in the future easily via git push heroku master. But why not take things a step further and enable continuous integration with automated deployment?