How to Deploy your Node.JS Website to Heroku
Tags: heroku, deploy, webdev
Published: 2021-04-15 15:07:48

Want to deploy your website to Heroku?

Here are the steps to do exactly that:

### Requirements:

* Node.js and npm installed.
* An existing Node.js app.
* A free Heroku account.
* Having a Node.JS version higher than Version 8
* Having NPM Installed. It is installed with Node so don't worry :)
* Having Git Installed
* Be in your current project directory in the CLI. If not, use <code>cd yourProject</code>

Assuming that you have already done that, let's start.

# 1. Downloading Heroku.

Download Heroku for your Operating system from <a href="https://devcenter.heroku.com/articles/getting-started-with-nodejs#set-up">Here</a>

Alternatively, if you are on a macbook, use the command:

```
brew install heroku/brew/heroku
```
# 2. Login with Heroku

Once you are done installing heroku, you the command

```
heroku login
```

to connect your heroku account with the CLI.

# 3. Changing the Port

Currently, your project is on a local server. But we don't want that to happen. Since we are deploying it to heroku, add this code to your Node.JS app:

```js
app.listen(process.env.PORT, function() {
  console.log("Listening to port 3000");
});
```

If you want it to listen at Port 3000 as well as heroku's servers, replace the code with this:

```js
app.listen(process.env.PORT || 3000, function() {
  console.log("Listening to port 3000");
});
```

# 4. Creating a Procfile

Now, we need to define a Procfile

This is the file that heroku is going to check to see how to launch your app.

Create a Procfile **without** Any extensions like <code>.txt</code> or <code>.rtf</code>

The name should only be "Procfile".

In your procfile add this:

```js
web: node yourJsFileName.js
```

# 5. Initialize Git and GitHub

Next, use the command:

```cli
git init
```

It will initialize an empty git repository for you

Then, use:

```cli
git add .
```

Now, to commit your changes, use:

```cli
git commit -m "Your Message"
```

# 6. Deploying your App

The next step is to deploy your app to heroku.

Next use the command:

```cli
heroku create
```

To create a new heroku project.

Then, it will give you a link to visit your website. For now, it will only Display: "Heroku | Welcome to your New App!" Don't worry as this is a regular part of the process.

Now, to deploy your code, use the command

```cli
git push heroku master
```

This will push our local version that's been stored using git to heroku.

It will take a couple of minutes to complete the process.

As a success message, it should show you 

```
remote: Verifying deploy...done.
```

Now, refresh the link that they showed you previously and VOILA your content displays. If this happened, then give yourself a pat on the back.

If you get an error like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrkuzwuyisfm4pfjgitf.png)

Not to worry!

This means that the website is still taking some time to upload. Try after some time. Then, you will see your Node.JS website up and LIVE!

Thank You For reading this blog.

If you want to deploy your website on other platforms, check out the different blogs in this series:

{% post https://dev.to/code2rithik/how-to-deploy-your-website-using-github-pages-34hc %}

{% post https://dev.to/code2rithik/how-to-deploy-your-website-using-vercel-4499 %}

{% post https://dev.to/code2rithik/how-to-deploy-your-website-to-netlify-2f1e %}

If you liked it then do it share it with your friends and remember...

# Keep Coding Y'All 👨🏻‍💻