ยท hands on
Run Node.js apps on Heroku with TypeScript
This article provides a step-by-step guide on how to get started with Heroku, a polyglot platform. It covers topics such as setting up a Node.js environment, connecting a Git repository, writing a Node.js application, connecting a GitHub repository, getting logs, running CLI apps, and working with databases.
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.
Contents
- Getting Started on Heroku
- Bootstrap Node.js environment
- Connect Git repository
- Write Node.js application
- Connect GitHub repository
- Get logs
- Run CLI apps
- Databases
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)
- Run
heroku --version
to see if it works (I tested with v6.15.5) - Run
heroku login
- Run
heroku whoami
to see if you are logged in - Run
heroku create --region eu --buildpack heroku/nodejs
to create a Node.js app on Heroku in a European data center (your app will get a URL likehttps://app-name-number.herokuapp.com/
) - Run
heroku open -a app-name-number
to 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 npm start
).
Note: Heroku will also run npm run build
by default before running npm start
.
Write Node.js application
When 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:
- Run
npm i --save-dev typescript
- Run
npm i --save express @types/express
- Run
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.
Get logs
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 process.env.npm_package_name
.
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:
2019-03-19T22:57:01.212233+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=app-name-number.herokuapp.com request_id=7832
926b-a547-4927-9895-ea8a12e42765 fwd="91.10.153.93" dyno= connect= service= status=503 bytes= protocol=https
2019-03-19T22:57:49.960888+00:00 heroku[web.1]: State changed from starting to crashed
2019-03-19T22:57:49.863438+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-03-19T22:57:49.863489+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-03-19T22:57:49.943431+00:00 heroku[web.1]: Process exited with status 137
Databases
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 package.json
file:
Backups
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.