Deploy from Terminal
You don't need a GitHub repository to deploy an application to Heroku. There may be cases where you want to deploy a local development without creating a GitHub repository. This section teaches you how to use Heroku CLI for that.
First, download and install Heroku CLI by following this guideline.
Next, you must link your Heroku CLI to your Heroku account. This is done by loggin to your account through the command-line interface. Please follow the instructions here.
We are going to create a very simple Express app locally before deploying it to Heroku. Create a folder heroku-cli-demo
. Open the terminal and change the directory to heroku-cli-demo
folder. Run npm init -y
to initiate a Node project. Follow this by running npm install express
.
Heroku does not need GitHub but it does need Git! So, before going any further, create a .gitignore
file inside heroku-cli-demo
with the following content:
.DS_Store
.vscode/*
.idea/*
node_modules/*
Next, head over to the terminal and run the following commands:
git init
git add .
git commit -m "Initial Commit"
Create the index.js
file inside heroku-cli-demo
with the following content:
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.send("Hello Heroku!")
});
app.listen(port, () => {
console.log(`Express app listening at port: ${port}`);
});
Notice the statement const port = process.env.PORT || 5000;
that allows us to use this script both locally and on Heroku by dynamically changing the port number.
Create the Procfile
file inside heroku-cli-demo
with the following content:
web: node ./index.js
Go to the terminal and run the following commands:
git add .
git commit -m "App is ready for deployment"
We can now deploy the Express app to Heroku with two simple commands!
First, create a Heroku App using CLI. In the terminal, run the following command:
heroku create
Here is the output on my computer
Creating app... done, ⬢ sleepy-ocean-40199
https://sleepy-ocean-40199.herokuapp.com/ | https://git.heroku.com/sleepy-ocean-40199.git
Notice an app has been created with a randomly generated name and it is now available at https://sleepy-ocean-40199.herokuapp.com/. The URL will be different for you.
Second, deploy the local repository to Heroku App. In the terminal, run the following command:
git push heroku master
This command pushes your code to the Heroku app and triggers a build followed by deployment. Here is the output on my computer
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 5.22 KiB | 5.22 MiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote: NPM_CONFIG_LOGLEVEL=error
remote: NODE_ENV=production
remote: NODE_MODULES_CACHE=true
remote: NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote: engines.node (package.json): unspecified
remote: engines.npm (package.json): unspecified (use default)
remote:
remote: Resolving node version 12.x...
remote: Downloading and installing node 12.19.0...
remote: Using default npm version: 6.14.8
remote:
remote: -----> Installing dependencies
remote: Installing node modules
remote: added 50 packages in 1.131s
remote:
remote: -----> Build
remote:
remote: -----> Caching build
remote: - node_modules
remote:
remote: -----> Pruning devDependencies
remote: audited 50 packages in 0.79s
remote: found 0 vulnerabilities
remote:
remote:
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 22.9M
remote: -----> Launching...
remote: Released v3
remote: https://sleepy-ocean-40199.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/sleepy-ocean-40199.git
* [new branch] master -> master
My app is now up and running at https://sleepy-ocean-40199.herokuapp.com/.
From this point on, every time I make a change to the local repository, I must commit those changes and then run the command
git push heroku master
to trigger a deployment on Heroku.
Note: you can rename the app from the randomly generated name to something else. The instruction are provided here.