Deployments
So you’ve got a cool Node.js app that you want to deploy on Heroku? No problemo, mate.
Heroku is an awesome platform for hosting and deploying web applications.
Here, we’ll show you how to take your Node.js app and get it up and running on Heroku in simple steps.
First things first, to follow along with this tutorial, you’ll need to have the following things set up and ready to go:
Node.js and npm installed on your computer.
An existing Node.js app.
A free Heroku account.
The Heroku CLI.
You can sign up for a free Heroku account here: https://signup.heroku.com/
You can download the Heroku CLI here: https://devcenter.heroku.com/articles/heroku-cli
Declare app dependencies
The package.json file is used to declare dependencies that should be installed with your application.
To create a package.json file for your app, navigate to the root directory of your app and run the command npm init.
This command will walk you through creating a package.json file.
You can skip any of the prompts by leaving them blank.
cd my-app npm init npm install express
Install dependencies
Use npm install <pkg> to install dependencies.
This command will install the package and also add it as a dependency in the package.json file.
For example, to install express, you would type npm install express.
npm install express
Make sure that you are not relying on any system-level packages.
Missing dependencies in your package.json file will cause problems when you try to deploy to Heroku.
To troubleshoot this issue, on your local command line, type rm -rf node_modules; npm install –production, and then try to run your app locally by typing heroku local web.
If a dependency is missing from your package.json file, you should see an error that says which module cannot be found.
Specify the version of Node.js
The version of Node.js that will be used to run your application on Heroku should be defined in your package.json file.
You should always specify a Node.js version that matches the runtime you’re developing and testing with.
To find your version type node –version.
"engines": { "node": "16.x" },
In the Node.js versioning scheme, odd versions are unstable, and even versions are stable.
The stable branch takes bug fixes only.
Add a start script
To determine how to start your app, Heroku first looks for a Procfile.
If no Procfile exists for a Node.js app, Heroku will attempt to start a default web process via the start script in your package.json.
The command in a web process type must bind to the port number specified in the PORT environment variable.
If it does not, the dyno will not start.
"scripts": { "start": "node index.js" },
Create a .gitignore file
We do not recommend checking node_modules into Git because this will cause the build cache not to be used.
For more information, see build behavior.
Prevent build artifacts from going into revision control by creating a .gitignore file that looks something like this:
/node_modules npm-debug.log .DS_Store /*.env
Commit your changes to Git
After you commit your changes to Git, you can deploy your app to Heroku.
git init git add . git commit -m "first commit"
Log in to Heroku
Log in to Heroku from the command line using the heroku login
Deploy Your Application to Heroku
After committing your changes to Git, you can deploy your app to Heroku.
First, make sure you have logged in to your Heroku account through the Heroku CLI using the heroku login command.
Then, create a new Heroku app using heroku create.
heroku login heroku create
This will create a new Heroku app with a unique name, and add a new remote Git repository to your local Git repository.
Finally, push your local Git repository to the remote Heroku repository using git push heroku main. This will deploy your app to Heroku.
git push heroku main
Once your app is deployed, you can open it in your browser using heroku open.
heroku open
Congratulations! You have successfully deployed your Node.js app to Heroku. You can now share your app with the world and use it in production.
Conclusion
Deploying your Node.js app to Heroku is a great way to make it accessible to the world.
Heroku provides a free platform to host and scale your applications with ease.
By following the steps outlined in this tutorial, you should be able to deploy your Node.js app to Heroku in no time.
Remember to keep your dependencies up to date, and make sure your app is secure and performant.
Peace out!