Deploying a NodeJS application with Heroku using git
The first thing you need to do is to set up your Heroku account. To do this, navigate to (https://signup.heroku.com/). On this page, you will see this form:
On this page, you input the right details and create your account for free. After this, you will be taken to the page below that shows a message that a verification mail has been sent to your email address.
Now, you can proceed to your email on your computer. If you provided the right email, you will see a link to click on in your email to verify your account. Once you click on this link, you will be redirected to a page to set your password:
After you set your password, you will see a page like an image below:
Now you can click on the button “Click here to proceed”. You will be taken to the terms and condition page like this page below:
After accepting, you will then see your dashboard as the image below:
Booyah, you have created your Heroku account and now you can proceed to create your first Heroku app.
Indeed, you need an understanding of Git to deploy to Heroku using git.
Now, you can go to your project, if you are using Vscode, you can open the terminal in the project, if not, then, cd to your project in the terminal.
In your terminal, you have to initiate git. Moreover, it is important to note that you must have git installed on your computer.
Now, proceed to run the git init command in your terminal(this is assuming that you have not committed your project to GitHub yet). You will see this:
Also now, you can go back to your Heroku dashboard created to create a new app.
Click on the new button on the right top corner. On clicking this, the page to where you add the app name and location to create the app will be rendered to you. See below to see how the page looks:
Note: You need to give a unique name that has not been chosen as the app name. The sweet thing is, worry not, because if you input a name that is chosen, an error will show up that name is not available like this:
Also, select the country either USA or EUROPE. It is believed and said that you should choose the closest in terms of proximity to the end-users of your application. Then click on the create app.
Yaaaah!!!! we are almost there!
After creating this app, you will be redirected to a page like this:
After doing this, you have to install the Heroku CLI. If you are on Mac, run this command in your terminal: $ brew tap heroku/brew && brew install heroku
If you are running on Linux, you will have to run this: $ sudo snap install — classic heroku
For Windows users, you can download via this link(https://devcenter.heroku.com/articles/heroku-cli). After this, follow the instructions and install the Heroku CLI on your computer. This can take some seconds to install, especially on windows.
To confirm that you have installed the Heroku CLI on your computer, run this command on your terminal, $ heroku — version
You will see an output like this:
After this, you have to log in to your Heroku dashboard. Remember the login you created at the beginning of this article right? Yes, that is the same login you will use but now, how?
Run this command $ heroku login in your terminal
Here, press any key on your keyboard to log in. After doing this, from your terminal, you will be redirected to your browser and from there, you will log in.
Click on this login page and you will see that you are logged in to your terminal. Once you are logged in, you will see this:
Now go back to your terminal and you will see that you are logged.
In your project that you initiated git the other time, you can now to run: $ heroku git: remote -a “name of the project”. The name of the project is the name you created.
Then proceed to run the $ git add . command in the project to add your files. After running this command, you will see adding of the files in your terminal like this:
After adding the files, you need to commit the project the same way you do when you want to push your project to GitHub. Run this command in the same terminal:
$ git commit -am “first commit”
The message in the quote can be anything but we are using this for the purpose of this article. After you are done running this command, your terminal looks like this:
So yeah you have come this far, let’s go to the final part which is to PUSH.
Run the push command: $ git push heroku master in your terminal
Now after this is done, you will receive the link to your website in this way: “projectname.herokuapp.com”.
Booyah we did it. You have deployed your NodeJS app to Heroku and you can share your app with others.