With Heroku, you can deploy your Express Node.js application to production in just a few steps. In this post, I'll show you a step-by-step guide to deploying your Express Node.js application to Heroku.
To quickly get started you can use my repo template simeg/express-heroku-example.
What is Heroku?
Heroku is a Platform as a Service (PaaS), and should not be confused with Service as a Service (SaaS). It offers a hobby plan where you can deploy your applications for free, with some limitations.
For all my hobby website projects I use Heroku. I've created things like sudoku-js and impossible-tic-tac-toe. See the About sections for links to the applications.
First, install the Heroku CLI. If you're on macOS run
$ brew tap heroku/brew && brew install heroku
Otherwise, head over to Heroku's website.
Deploy Node.js Application to Heroku
Now that you have the CLI installed, we can start writing some code. We will use a minimal example with an HTTP Express server.
Bootstrap a Node.js application with
npm init. Then add Express as a dependency with
npm i --save express.
Now let's look at our slim Express server in
You can read more about Express here.
This HTTP server is simple. It has one
GET endpoint which returns the
200 and the text
Now that we have the server ready, we need some extra things to be able to deploy it to Heroku. First of all, we need a
This is the file that Heroku reads when it starts the application. As you can see the file runs
npm start, so we need to create that too. We add it to
Also, notice the
engines section. This is for Heroku to know what runtime to use to run your application. You can see what Node.js versions Heroku support on this site.
Deploy to Heroku
There are a few ways to deploy to Heroku. We will use git which is the easiest way.
Now that all the code is written we need to commit it.
$ git add . $ git commit -m "Initial commit"
Then we need to create an application on Heroku.
$ heroku create
This command also adds a git remote called
heroku. This remote is where we push to deploy our application. Let's do that now!
$ git push heroku main
At this point, Heroku will try to figure out what build pack to use. Essentially, what type of application are you deploying? Because we have a
package.json file in our root, it knows it's a Node.js application.
When the command is done it will output a URL. Let's open it!
... https://thawing-beyond-32509.herokuapp.com/ deployed to Heroku ...
And we can see
Hello World! in the browser. Easy as pie!
Now you can check the logs for your application.
$ heroku logs --tail
Now you know how to deploy a Node.js application to Heroku. Heroku provides great tooling to quickly get something up and running. But this is just the start! Express allows you to build complex web applications. And with Heroku, you can quickly deploy them to production.
Check out Heroku's Best Practices for Node.js Development for tips and tricks. And their page about Node.js is also useful.
Connect with me on Twitter, LinkedIn, or GitHub
Originally published at prplcode.dev
Top comments (2)
One thing I love about Heroku is that you can create lots of small projects and host them for free <3 The web "dynos" (that's how they call them) go to sleep when you stop using them and they don't count agains the "free dyno hours" they give out for free every month.
I always recommend deploying an express API like the one you described, a Strapi headless CMS or maybe something more complex like a Medusa e-commerce backend on Heroku. This pairs very well with Next.js and static websites. Specially if you deploy these on Vercel, Netlify or CloudFlare for free :)
Yes, exactly! Heroku is perfect for prototyping projects. And so easy to use.