How to Deploy a Node.js App to Heroku

In this tutorial, we will learn how to deploy a Node.js application to Heroku. Heroku is a cloud platform as a service (PaaS) that allows developers to deploy, manage, and scale applications easily.

By the end of this tutorial, you will have your Node.js application running on Heroku, ready to be accessed by users.

Prerequisites

Before we begin, make sure you have the following:

  • A Heroku account. If you don’t have one, sign up for free at Heroku.
  • Node.js and npm installed on your machine.

Step 1: Set Up Your Node.js App

First, create a new directory for your Node.js application and navigate into it:

mkdir my-node-app
cd my-node-app

Next, initialize a new Node.js project by running the following command:

npm init -y

This command will generate a package.json file in your project directory.

Now, let’s create a simple Node.js application. Create a new file called index.js and add the following code:

const http = require('http');

const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!n');
});

server.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

This code sets up a simple HTTP server that listens on the specified port (either the one provided by Heroku, or 3000 if running locally). It responds with a plain text message: “Hello, world!”.

Step 2: Install Required Dependencies

Since we are running a Node.js application, we need to install the required dependencies. In this case, we only need the http module, which is included by default with Node.js.

Step 3: Create a Git Repository

Heroku uses Git for deployment, so we need to initialize a new Git repository in our project folder:

git init

Next, create a .gitignore file and add the following lines to it:

node_modules/
npm-debug.log

These lines tell Git to ignore the node_modules directory and any npm-debug.log files.

Step 4: Deploy to Heroku

Now it’s time to deploy our application to Heroku.

4.1: Install the Heroku CLI

To interact with Heroku from the command line, we need to install the Heroku CLI (Command Line Interface). Visit the following link to download and install it: Heroku CLI.

Once installed, open a new terminal and run the following command to login to Heroku:

heroku login

This will open a browser window where you can enter your Heroku credentials.

4.2: Create a New Heroku App

Next, run the following command to create a new Heroku app:

heroku create

This will generate a unique name for your app and add a new remote to your Git repository.

4.3: Push Your Code to Heroku

Now, we can deploy our application to Heroku. Run the following command:

git push heroku master

This command will push the code from our local repository to Heroku, triggering a build and deployment process.

4.4: Access Your App

Finally, you can access your deployed app by running the following command:

heroku open

This will open your app in the default browser.

Step 5: Updating Your App

If you make changes to your code and want to update your app on Heroku, you can simply push the changes to the master branch on Heroku:

git push heroku master

Heroku will detect the changes, build the updated version of your app, and restart it.

Conclusion

Congratulations! You have successfully deployed a Node.js application to Heroku. You can now scale your app, add custom domains, and take advantage of many other features provided by Heroku.

Remember to commit any changes to your code to your Git repository and push them to Heroku using git push heroku master whenever you want to update your app.

Now that you have a deployed application, you can start adding more features and functionalities to make it useful for your users.

Happy coding!

Related Post