Table of contents
- 1. Getting Started on Heroku
- 2. Bootstrap Node.js environment
- 3. Connect Git repository
- 4. Write Node.js application
- 5. Connect GitHub repository
- 6. Get logs
- 7. Run CLI apps
- 8. Databases
- 8.1. Backups
The best way to get started on the Heroku polyglot platform is to follow their fantastic introduction. It’s also worth reading about their supported environments, deployment tasks and European deployment region. The second best advice is to follow our quick setup guide.
Getting Started on Heroku
To speed things up, I am providing a list of resources which I found useful when deploying my first Node.js web applications on Heroku.
Bootstrap Node.js environment
- Download the Heroku CLI (or Heroku Toolbelt)
heroku --versionto see if it works (I tested with v6.15.5)
heroku whoamito see if you are logged in
heroku create --region eu --buildpack heroku/nodejsto create a Node.js app on Heroku in a European data center (your app will get a URL like
heroku open -a app-name-numberto see your application in a browser
Connect Git repository
By default, web applications created on Heroku (with
heroku create) come with their own Git repository. If you are starting completely from scratch, then you can follow these steps to push your own code to Heroku’s Git repository:
If you now execute
heroku open, you will see an “Application error” because you need to specify a way to start a Node.js process (most likely through
Note: Heroku will also run
npm run build by default before running
Write Node.js application
npm init -y was executed, a
package.json file has been created. We will make some adjustments to that file and create a web application based on the Express web framework:
npm i --save-dev typescript
npm i --save express @types/express
npx tsc --init
- Modify source code to match the following files.
Deploy the latest code changes:
Connect GitHub repository
To run deployments from your GitHub repository, you need to add Heroku’s Git repository to your cloned GitHub repository:
You can also connect your Heroku application with code from GitHub by using Heroku’s GitHub Deployments from the app dashboard.
Run CLI apps
If you want to run a pure command-line app which does not serve a webpage, then you can change your use a “worker” dyno instead of a “web” done. Just place a file called
Procfile in the root of your project and define the start script for the “worker” dyno:
Tip: Prefer “npm start” over “node dist/main.js” to run in the npm context and to have access to environment variables like
Next thing you should do is to scale down your “web” dyno, if you just want to run one “worker” dyno:
Heroku runs health checks on the web domain of your application. That’s why you need to scale down the “web” dyno if you just use a CLI app because otherweise the web health check will fail (see example below) and Heroku will kill your application:
Databases on Heroku are handled as “Add-ons”. You can get a “Heroku Postgress“ database for free. Connecting your Node.js application with it is super simple. Just add the database from your Heroku application dashboard as “Add-on” and Heroku will handle the rest for you and provide a
process.env.DATABASE_URL variable that can be used to connect via object-relational mappers like TypeORM.
Note: You can also get the current connection properties (database name, user, password, port, etc.) from the settings panel of your data store on data.heroku.com but be aware that Heroku rotates credentials periodically so it’s advisable to rely on the database connection url instead of a username and password combination.
You will also need to have these dependencies in your
Here is how you can make backups of your data store on Heroku:
- You can find your databases on data.heroku.com
- Use the following command to turn binary database backups into plain text:
pg_restore backup.bin > backup.sql
If you look for a free tool to connection to your Heroku Postgres database, then have a look at pgAdmin.