We normally construct many projects and applications as novices, but this is only half of the equation.
We also want the rest of the world to be able to use and test these applications, whether it’s for a presentation, a prospective recruiter, or just to get it out in front of everyone.
We’ll need to put these apps on a public server to do this.
Heroku is a fully managed cloud-based platform as a service (PaaS) for developing, deploying, and maintaining apps.
The platform is adaptable and built with DX in mind to accommodate your and your team’s chosen development method while keeping you focused and productive.
Heroku is used by developers, teams, and enterprises of all kinds to launch, manage, and grow projects. Whether you’re creating a small prototype or a mission-critical product, Heroku’s fully-managed platform provides the most straightforward way to deliver apps rapidly.
One of Heroku’s key features is the ability to deploy, manage, and scale projects written in your preferred programming languages [Node, Ruby, Python, Java, PHP, Go, and others].
In this post, I’ll teach you how to deploy an existing Node.js project on Heroku.
Prerequisites
This article’s best practices assume that you have:
- Installed Node.js with npm.
- A Heroku account is completely free.
- A Node.js application that already exists
- Heroku’s command-line interface (CLI).
1. Declare your application’s dependencies
The package.json file specifies which dependencies should be installed with your project.
Run the command npm init in the root directory of your project to generate a package.json file for it. It’ll show you how to make a package.json file.
By leaving them blank, you can skip any of the questions. On Windows, launch the Git Bash program to launch a command shell.
The resulting package.json file is as follows:
Use `npm install pkg>` to install dependencies.
This will install the package as well as make it a dependency on the `package.json` data file To install express, for example, type `npm install express`.
Make certain that no system-level packages are being used. Your package lacks dependencies. When attempting to deploy to Heroku, the JSON file will cause issues.
To troubleshoot this issue, execute `rm -rf node modules; npm install — production` on your local command line, and then try to launch your app locally by entering Heroku local web.
If one of your package’s dependencies is missing .json file, you should notice an error message indicating which module could not be located.
2. Specify the node’s version
Your package.json file should also include the Node.js version that will be utilized to execute your application on Heroku.
You should always use the Node.js version that corresponds to the runtime you’re working with. Type node —version to find your version.
This is what your package.json file will look like:
Now that the dependencies have been installed and the node version to be used has been specified, the package.json file should look like this:
3. Create a Procfile
Make a Procfile in the root of your folder (make sure there is no extension and the letter “P” is capitalized).
It is in the Heroku file that you will configure the Dynos settings, such as how Heroku will start the node js web server after uploading the app.
The Procfile in the sample app you deployed looks like this:
4. Create your app and test it locally
To install the dependencies you indicated in your package.json file, run the npm install command in your local app directory.
Use the Heroku local command, which is included in the Heroku CLI, to start your app locally.
You should now be able to access your app at https://localhost:5000/.
5. Create artifacts
Create a .gitignore file that looks something like this to keep build artifacts out of revision control:
6. Deploy your node.js application to Heroku
The following instructions will deploy your application to Heroku.
7. Heroku Logs
You can also use one of the logging commands to get information about your running program. This is quite helpful for troubleshooting issues.
Conclusion
Congratulations, your Node.js application is now live on Heroku. It is a cloud platform that allows businesses to create, distribute, manage, and grow apps.
Heroku was there for us in this new era of serverless and containerization, where we are just a click away from deploying our whole project, and it is still helping millions of users deploy their projects with simplicity.
Leave a Reply